Java 在用户输入的方法中添加两个整数
有人能告诉我为什么当我显示doSum方法的和时,用户输入的两个整数没有被相加,它只显示最后一个被相加的整数。该程序的要点是使用方法重载从用户处获取两个整数输入,测试输入是否为getInt中的整数,不带low和high参数,然后在getInt中使用high和low来测试整数是否在一定范围内。然后将doSum调用sum方法,并显示输入的整数及其总和Java 在用户输入的方法中添加两个整数,java,methods,Java,Methods,有人能告诉我为什么当我显示doSum方法的和时,用户输入的两个整数没有被相加,它只显示最后一个被相加的整数。该程序的要点是使用方法重载从用户处获取两个整数输入,测试输入是否为getInt中的整数,不带low和high参数,然后在getInt中使用high和low来测试整数是否在一定范围内。然后将doSum调用sum方法,并显示输入的整数及其总和 public static void main(String[] args) { Scanner key = new Scanner (S
public static void main(String[] args)
{
Scanner key = new Scanner (System.in);
String str; // to hold users input
summation(key);
// summation();
}
public static void summation (Scanner scan)
{
int low, high, sum;
low = getInt (scan, "enter and integer", 1, 100);
high = getInt (scan, "enter and integer", 1, 100);
sum = doSum(low,high);
// System.out.println("doSum " + [low,high] + "sum is " + sum );
}
public static int getInt(Scanner scan, String prompt, int low, int high)
{
//call method getInt
int integer = getInt (scan, "enter in integer");
if (integer >= low && integer <= high && high >= low)
{
System.out.println("input is in range");
}
else
{
//if integer is not in range
while (integer <= low || integer >= high || high<= low)
{
System.out.println("error interger must be higher than or equal to low and lower than or equal to high, renter");
integer = getInt (scan, "enter in integer");
}
}
return integer;
}
public static int getInt(Scanner scan, String prompt)
{
String str; // to hold users input
int integer = 0;
System.out.println (" Enter an integer ");
hasInt(scan);
if (scan.hasNextInt())
{
str = scan.nextLine();
System.out.println("input is an integer");
integer = Integer.parseInt(str);
}
// if integer is not an integer, display error and repeat
else
{
while (!scan.hasNextInt())
{
System.out.println (" error, must be an integer, renter: ");
str = scan.nextLine();
}
}
return integer;
}
public static int doSum (int start, int end)
{
int sum = start++;
start = start + 1;
while (start <= end)
{
sum = start;
start = start + 1;
}
System.out.println(sum);
return sum;
}
public static boolean hasInt(Scanner scan)
{
return scan.hasNextInt();
}
}getInt方法不使用hasInt方法或prompt参数。另外,我建议在hasInt测试的否定上使用一个循环。我想你想要像这样的东西
public static int getInt(Scanner scan, String prompt) {
System.out.println(prompt);
while (!hasInt(scan)) {
System.out.println("Error, must be an integer, "
+ "please re-enter: ");
scan.nextLine();
}
return scan.nextInt();
}
public static int doSum (int start, int end) {
return start + end;
}
另外,doSum应该是
public static int getInt(Scanner scan, String prompt) {
System.out.println(prompt);
while (!hasInt(scan)) {
System.out.println("Error, must be an integer, "
+ "please re-enter: ");
scan.nextLine();
}
return scan.nextInt();
}
public static int doSum (int start, int end) {
return start + end;
}
而不是像这样在屏幕上打印:
System.out.println("doSum " + [low,high] + "sum is " + sum );
试试这个:
System.out.println("doSum " + low + "," + high + "sum is " + sum );
如果我正确理解你的doSum方法。我认为你实际上没有在方法中保持一个连续的和,因此你总是得到最后一个数字,因为你在循环中分配了最后一个数字
public static int doSum (int start, int end)
{
int sum = start++;
start = start + 1;
while (start <= end)
{
sum += start;
start = start + 1;
}
System.out.println(sum);
return sum;
}为什么您认为您的doSum方法是正确的?你要做的就是递增start,直到它等于end。为什么你不能只做开始+结束谢谢我照你说的做了,它成功了,我甚至不知道老师给我们的while循环是什么,使用相同的代码我知道如何确保用户的第二个整数等于或高于第一个整数吗?我认为它是在getInt方法中用high和low完成的