创建条件语句以对应扫描仪输入
Creating a conditional statement to correspond with scanner input
我正在为 class 开发程序。
我需要使用扫描仪读取用户的输入class。
然后我需要将这些数据传递给带有 while 循环的数组列表。
在我的 while 循环中,我需要一个条件语句来检查输入的是 0 还是负数。
如果用户输入负数或 0,则循环结束,代码进入下一个过程...
我目前面临的问题是:
-我所有的输入值都没有被处理
-我必须输入值0 3次才能退出循环
-0 被传递到我不想要的数组列表
到目前为止,这是我的代码:
import java.util.*;
public class questionAvg3
{
public static void main(String[]args)
{
Scanner input_into = new Scanner(System.in);
ArrayList<Integer> collector = new ArrayList<Integer>();
System.out.println("Enter 0 or a negative number to end input");
System.out.println("Enter a positive integer to populate the arraylist");
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
int minValue = collector.get(0);
int maxValue = collector.get(0);
//int avgValue = collector.get(0);
//int total = 0;
for(Integer i: collector){
if( i < minValue) minValue = i;
if( i > maxValue) maxValue = i;
}
System.out.println("The max value int is: " + maxValue);
System.out.println("The min value int is: " + minValue);
}
}
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
这确实需要3个整数才能传递。因为 nextInt
总是寻找下一个输入值。
你需要的是
int input = input_into.nextInt();
while ((input !=0) || (input < 0)){
System.out.println("Type another int or exit");
collector.add(input);
input = input_into.nextInt();
}
在循环内使用一个变量并用它来测试循环继续条件
int variable=input.nextInt(); //get initial input
while (variable<=0)
{
//get next input
variable=input.nextInt();
}//end while
这将循环直到输入 <=0
你的问题就在这里:
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
你必须输入 3 次 0 的原因是因为你正在调用 input_into.nextInt()
3 次!
这意味着您的程序正在等待一个整数,根据您的比较评估输入,然后再做一次,最后一次为您的 collector.add()
.
我认为了解如何更好地使用比较运算符对您很重要。
例如,当您说 (input_into.nextInt() !=0) || (input_into.nextInt() < 0)
时,您实际上是在说该数字小于零或大于零。
由于您的标志值是 <= 零的任何数字,因此您只想在输入大于零时继续。这为您提供了以下内容
int input = input_into.nextint();
while (input > 0){
System.out.println("Type another int or exit");
collector.add(input);
input = input_into.nextint();
}
我正在为 class 开发程序。
我需要使用扫描仪读取用户的输入class。
然后我需要将这些数据传递给带有 while 循环的数组列表。
在我的 while 循环中,我需要一个条件语句来检查输入的是 0 还是负数。
如果用户输入负数或 0,则循环结束,代码进入下一个过程...
我目前面临的问题是:
-我所有的输入值都没有被处理
-我必须输入值0 3次才能退出循环
-0 被传递到我不想要的数组列表
到目前为止,这是我的代码:
import java.util.*;
public class questionAvg3
{
public static void main(String[]args)
{
Scanner input_into = new Scanner(System.in);
ArrayList<Integer> collector = new ArrayList<Integer>();
System.out.println("Enter 0 or a negative number to end input");
System.out.println("Enter a positive integer to populate the arraylist");
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
int minValue = collector.get(0);
int maxValue = collector.get(0);
//int avgValue = collector.get(0);
//int total = 0;
for(Integer i: collector){
if( i < minValue) minValue = i;
if( i > maxValue) maxValue = i;
}
System.out.println("The max value int is: " + maxValue);
System.out.println("The min value int is: " + minValue);
}
}
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
这确实需要3个整数才能传递。因为 nextInt
总是寻找下一个输入值。
你需要的是
int input = input_into.nextInt();
while ((input !=0) || (input < 0)){
System.out.println("Type another int or exit");
collector.add(input);
input = input_into.nextInt();
}
在循环内使用一个变量并用它来测试循环继续条件
int variable=input.nextInt(); //get initial input
while (variable<=0)
{
//get next input
variable=input.nextInt();
}//end while
这将循环直到输入 <=0
你的问题就在这里:
while ((input_into.nextInt() !=0) || (input_into.nextInt() < 0)){
System.out.println("Type another int or exit");
collector.add(input_into.nextInt());
}
你必须输入 3 次 0 的原因是因为你正在调用 input_into.nextInt()
3 次!
这意味着您的程序正在等待一个整数,根据您的比较评估输入,然后再做一次,最后一次为您的 collector.add()
.
我认为了解如何更好地使用比较运算符对您很重要。
例如,当您说 (input_into.nextInt() !=0) || (input_into.nextInt() < 0)
时,您实际上是在说该数字小于零或大于零。
由于您的标志值是 <= 零的任何数字,因此您只想在输入大于零时继续。这为您提供了以下内容
int input = input_into.nextint();
while (input > 0){
System.out.println("Type another int or exit");
collector.add(input);
input = input_into.nextint();
}