发布网友 发布时间:2023-09-26 15:10
共2个回答
热心网友 时间:2024-11-26 05:52
package com.ie;
import java.io.IOException;
public class Add {
private int firstNumber;
private int secondNumber;
public static void main(String args[]){
Add add = new Add();
System.out.println("请输入第一个数据:");
add.setFirstNumber(add.input());
System.out.println("请输入第二个数据:");
add.setSecondNumber(add.input());
System.out.println("求和结果为:\n"+add.sum());
}
//输入数据
public int input(){
int date = 0;
try {
for(;;){
byte temp[] = new byte[20];
System.in.read(temp);
if(this.isOK(Integer.parseInt(new String(temp).trim()))){
date = Integer.parseInt(new String(temp).trim());
break;
}else{
System.out.println("输入的数据要求在0-100之间,重新输入!!!");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return date;
}
//判断输入的数据是否在指定的范围内
public boolean isOK(int date){
if(date>=0&&date<=100){
return true;
}else{
return false;
}
}
//完成求和功能
public int sum(){
int start = 0;
int end = 0;
int sum = 0;
if(this.getFirstNumber()<=this.getSecondNumber()){
start = this.getFirstNumber();
end = this.getSecondNumber();
}else{
start = this.getSecondNumber();
end = this.getFirstNumber();
}
for(int temp = start; temp <= end; temp++){
sum = sum + temp;
}
return sum;
}
public int getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
public int getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
}
}
嗯好像不对啊,跟题目要求的有点儿差别
热心网友 时间:2024-11-26 05:52
用ifelse循环追问帮忙给一下完整的代码呗,本人小白