为什么当我尝试在第二次提示之后放置Z=input.nextInt();
时它不起作用但是当我把它放在第二个for循环时它起作用。
import java.util.Scanner;
public class FaresMarwan_150086 {
public static void main(String[] args) {
// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);
int M;// The normal dose entered by the user
int endValue;// The value that stops the loop
int sum;// sum of experiments entered by the user
int Z;
double percentage;// percentage of bad experiments
double warn = 0;// initialize rule violation
double exp = 0;// initialize experiment
System.out.print("Please, insert the normal dose in ml: "); // prompt
M= input.nextInt(); // read M from the user
System.out.println("Please, insert the set of experiments (3 integers per line, stop by 0 0 0): ");// prompt
for(endValue=0; endValue==0;) // loop till endValue is 0
{
// initialization phase
sum=0; // initialize sum
for(int count = 0; count<=2; count++) //loop 3 times
{
Z=input.nextInt(); //read z from user
sum=sum+Z; // Sum is sum plus Z
if(sum==0) // if the sum becomes 0
{
endValue++; // Increment endValue by 1
}// end if
}// end for
if(sum!=0) //if the sum is not 0
{
exp++; // Increment exp by 1
}
if(sum>M) // if sum is bigger that M
{
warn++; // Increment warn by 1
}
if(exp==0) // if exp is 0
{
exp++; // Increment exp by 1
}// end if
}// end for
// termination phase
percentage = (warn/exp)*100; // divide warn on exp and multiply that by 100
System.out.printf("The percentage of bad experiments is %.2f%% .\n" ,percentage);
}// end main method
}// end class FaresMarwan_150086
答案 0 :(得分:0)
input.nextInt();只接受一个int
根据您的评论,您似乎要求用户一次输入3个号码,例如空格,例如1 2 3
如果在for循环之前放置input.nextInt(),则Z = 1
但是当你将input.nextInt()放在for循环中时,它将获取你输入的3个整数,因为你调用了input.nextInt()3次。