
正文
java程序设计代码 java程序设计百度百科
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java程序设计,输入10个数字,,分别计算其中的奇数和与偶数和
参考代码
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int jishu=0;//存储奇数java程序设计代码的和
int oushu=0;//存储偶数的和
for (int i = 0; i 10; i++) {
System.out.println("请输入第"+(i+1)+"个数字");
Scanner sc = new Scanner(System.in);
int x = Integer.parseInt(sc.nextLine().trim());//从键盘读取数据java程序设计代码,转化成为数字
if(x%2==0){//如果除2余数是0java程序设计代码,说明是偶数
oushu+=x;
//oushu = oushu+x;//上面的代码等同于这个代码
}else{
jishu+=x;
}
}
System.out.println("奇数和java程序设计代码:"+jishu);
System.out.println("偶数和java程序设计代码:"+oushu);
}
}
相关问答
Q1: java程序设计 计算器 求代码
这段代码对你有帮助
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
/**
* 我的计算器。MyCalculator 继承于 JFrame,是计算器的界面
*/
public class MyCalculator extends JFrame {
private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
private JTextField textbox = new JTextField("0");
private CalculatorCore core = new CalculatorCore();
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
String label = b.getText();
String result = core.process(label);
textbox.setText(result);
}
};
public MyCalculator(String title) throws HeadlessException {
super(title); // 调用父类构造方法
setupFrame(); // 调整窗体属性
setupControls(); // 创建控件
}
private void setupControls() {
setupDisplayPanel(); // 创建文本面板
setupButtonsPanel(); // 创建按钮面板
}
// 创建按钮面板并添加按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(4, 5, 3, 3));
createButtons(panel, new String[]{
"7", "8", "9", "+", "C",
"4", "5", "6", "-", "CE",
"1", "2", "3", "*", "", // 空字符串表示这个位置没有按钮
"0", ".", "=", "/", ""
});
this.add(panel, BorderLayout.CENTER);
}
/**
* 在指定的面板上创建按钮
*
* @param panel 要创建按钮的面板
* @param labels 按钮文字
*/
private void createButtons(JPanel panel, String[] labels) {
for (String label : labels) {
// 如果 label 为空,则表示创建一个空面板。否则创建一个按钮。
if (label.equals("")) {
panel.add(new JPanel());
} else {
JButton b = new JButton(label);
b.addActionListener(listener); // 为按钮添加侦听器
panel.add(b);
}
}
}
// 设置显示面板,用一个文本框来作为计算器的显示部分。
private void setupDisplayPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(border);
setupTextbox();
panel.add(textbox, BorderLayout.CENTER);
this.add(panel, BorderLayout.NORTH);
}
// 调整文本框
private void setupTextbox() {
textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右对齐
textbox.setEditable(false); // 文本框只读
textbox.setBackground(Color.white); // 文本框背景色为白色
}
// 调整窗体
private void setupFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 当窗体关闭时程序结束
this.setLocation(100, 50); // 设置窗体显示在桌面上的位置
this.setSize(300, 200); // 设置窗体大小
this.setResizable(false); // 窗体大小固定
}
// 程序入口
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
MyCalculator frame = new MyCalculator("我的计算器");
frame.setVisible(true); // 在桌面上显示窗体
}
}
/**
* 计算器核心逻辑。这个逻辑只能处理 1~2 个数的运算。
*/
class CalculatorCore {
private String displayText = "0"; // 要显示的文本
boolean reset = true;
private BigDecimal number1, number2;
private String operator;
private HashMapString, Operator operators = new HashMapString, Operator();
private HashMapString, Processor processors = new HashMapString, Processor();
CalculatorCore() {
setupOperators();
setupProcessors();
}
// 为每种命令添加处理方式
private void setupProcessors() {
processors.put("[0-9]", new Processor() {
public void calculate(String command) {
numberClicked(command);
}
});
processors.put("\\.", new Processor() {
public void calculate(String command) {
dotClicked();
}
});
processors.put("=", new Processor() {
public void calculate(String command) {
equalsClicked();
}
});
processors.put("[+\\-*/]", new Processor() {
public void calculate(String command) {
operatorClicked(command);
}
});
processors.put("C", new Processor() {
public void calculate(String command) {
clearClicked();
}
});
processors.put("CE", new Processor() {
public void calculate(String command) {
clearErrorClicked();
}
});
}
// 为每种 operator 添加处理方式
private void setupOperators() {
operators.put("+", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.add(number2);
}
});
operators.put("-", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.subtract(number2);
}
});
operators.put("*", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.multiply(number2);
}
});
operators.put("/", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.divide(number2, 30, RoundingMode.HALF_UP);
}
});
}
// 根据命令处理。这里的命令实际上就是按钮文本。
public String process(String command) {
for (String pattern : processors.keySet()) {
if (command.matches(pattern)) {
processors.get(pattern).calculate(command);
break;
}
}
return displayText;
}
// 当按下 CE 时
private void clearErrorClicked() {
if (operator == null) {
number1 = null;
} else {
number2 = null;
}
displayText = "0";
reset = true;
}
// 当按下 C 时,将计算器置为初始状态。
private void clearClicked() {
number1 = null;
number2 = null;
operator = null;
displayText = "0";
reset = true;
}
// 当按下 = 时
private void equalsClicked() {
calculateResult();
number1 = null;
number2 = null;
operator = null;
reset = true;
}
// 计算结果
private void calculateResult() {
number2 = new BigDecimal(displayText);
Operator oper = operators.get(operator);
if (oper != null) {
BigDecimal result = oper.process(number1, number2);
displayText = result.toString();
}
}
// 当按下 +-*/ 时(这里也可以扩展成其他中间操作符)
private void operatorClicked(String command) {
if (operator != null) {
calculateResult();
}
number1 = new BigDecimal(displayText);
operator = command;
reset = true;
}
// 当按下 . 时
private void dotClicked() {
if (displayText.indexOf(".") == -1) {
displayText += ".";
} else if (reset) {
displayText = "0.";
}
reset = false;
}
// 当按下 0-9 时
private void numberClicked(String command) {
if (reset) {
displayText = command;
} else {
displayText += command;
}
reset = false;
}
// 运算符处理接口
interface Operator {
BigDecimal process(BigDecimal number1, BigDecimal number2);
}
// 按钮处理接口
interface Processor {
void calculate(String command);
}
}
Q2: 北大青鸟设计培训:Java程序员编写代码的技巧?
java程序员编写代码的技巧有哪些呢?Java程序员必须努力编写完美的代码,因此,为了编写干净的代码,必须知道如何编写才是真正的好方法,下面郑州郑州java软件开发为大家总结了一些帮助编写代码的小方法。
1、编写之前进行思考首先花10分钟,20分钟甚至30分钟的时间来思考你需要什么,以及哪些设计模式适合你将要编码的内容。
这个时候郑州郑州IT培训认为你会很高兴你花费的这些时间,当你必须改变或添加一些代码时,你不会担心花费的几分钟,而是会花更多的时间去琢磨。
2、代码进行注释没有什么比两个月后检查你的代码并不记得它被用来做什么更糟的了。
重要的都注释一下,如果是自己非常了解的内容,郑州北大青鸟建议这些可以选择不进行注释。
3、不要复制粘贴代码最后自己手写代码,复制粘贴代码会出现代码块重复的情况,并且不利于代码的解读。
4、学会测试代码测试、测试、测试你的代码。
不要等到你完成了程序才测试它,否则当你发现一个巨大的错误时,你会后悔之前的决定。
因为郑州计算机培训学校发现如果你没有进行每段代码测试,最后出现问题是很难找到的。
Q3: java程序设计代码:显示一个三位整数的各位数字 输入一个整数,分别显示其百位、十位和个位数字。
你好,按照你的要求代码如下,可以直接运行,并给出了运行结果:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// 获得用户输入
System.out.println("请输入一个数:");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
s.close();
// 判断是否是三位数
if (i 100 || i 999) {
System.out.println("不是三位数");
return;
}
// 输入结果
String str = String.valueOf(i);
System.out.println("百位:" + str.charAt(0));
System.out.println("十位:" + str.charAt(1));
System.out.println("个位:" + str.charAt(2));
}
}
运行结果:
请输入一个数:
134
百位:1
十位:3
个位:4
Q4: java程序设计,求代码 1.定义学生类,学生类有学号,姓名,语文成绩,数学成绩的属性和有参的构造
import java.util.Comparator;
public class Student implements ComparableStudent {
private int no;
private String name;
private String sex;
private int roomNo;
private double score;
public Student(int no, String name, String sex, int roomNo, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.roomNo = roomNo;
this.score = score;
}
public Student(int no, String name, String sex, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.score = score;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getRoomNo() {
return roomNo;
}
public void setRoomNo(int roomNo) {
this.roomNo = roomNo;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
if (this.no o.no) return 1;
else if (this.no o.no) return -1;
else return 0;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", roomNo=" + roomNo +
", score=" + score +
'}';
}
}
//性别比较器类
class SexComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getSex().compareTo(o2.getSex()) 0) return 1;
else if (o1.getSex().compareTo(o2.getSex()) 0) return -1;
else return 0;
}
}
//寝室号比较器类
class RoomNoComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getRoomNo() o2.getRoomNo()) return 1;
else if (o1.getRoomNo() o2.getRoomNo()) return -1;
else return 0;
}
}
//入学成绩比较器类
class ScoreComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getScore() o2.getScore()) return 1;
else if (o1.getScore() o2.getScore()) return -1;
else return 0;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1, "jack", "boy", 90);
Student s2 = new Student(5, "jack", "boy", 90);
Student s3 = new Student(4, "jack", "boy", 90);
Student s4 = new Student(2, "jack", "boy", 90);
ListStudent studentList=new ArrayList();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
Collections.sort(studentList);
System.out.println(Arrays.toString(studentList.toArray()));
}
}
Q5: java程序设计?
下面是BankAccount类的代码:
public class BankAccount {
private String name;
private double balance;
private int year;
private double rate = 0.01d;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getBalance() {
return balance;
}
public BankAccount() {
this.balance = 10d;
this.year = 1;
}
public BankAccount(double balance, int year) {
this.balance = balance;
this.year = year;
}
public void save(double balance, int year) {
this.balance = this.balance + balance;
this.year = year;
}
public void fetch(double balance) {
this.balance = this.balance - balance;
this.year = 0;
}
public double calcTotal() {
return this.balance * this.rate * this.year + this.balance;
}
}
下面是BankTest类的代码:
public class BankTest {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000d, 3);
account.setName("Tom");
account.save(2000d, 3);
System.out.println("该账户的姓名:" + account.getName());
System.out.println("该账户的存款额:" + account.getBalance());
System.out.println("该账户的总金额:" + account.calcTotal());
}
}
下面是运行结果:
麻烦您看一下,是否能够满足要求。
java程序设计代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java程序设计百度百科、java程序设计代码的信息别忘了在本站进行查找喔。






