I am a beginner in Java.
We have created a program that repeatedly enters values and the total value exceeds 100, or stops typing when it is entered 10 times and outputs the previous value.
I write using loop processing and array in Eclipse, but when I specified the conditions in while,
The following warning will appear:
·Operator < is undefined by the argument type Scanner, int
I think it's defined outside the scope, but
I'm not sure why the error occurs, so
Please tell me the solution.
Please.
ints[] = new int[100];
int input = 0;
Scannerscan;
// Enter a value from the keyboard
for(inti=0;i<10;i++){
System.out.println("Enter an integer");
scan = new Scanner (System.in);
s[i] = scan.nextInt();
}
// Stop input when input value exceeds 100 or input is made 10 times
while(scan<10&s<100){
}
// Output all entered numbers
}
Your code has the following bad points:
Your code is for(inti=0;i<10;i++){...}
and you are trying to write a condition in while(...){...}
to "stop" the input after 10 loops.
In your code, scan
represents an instance of type Scanner
.This doesn't reflect your intentions and sometimes indicate the number of inputs.Therefore, scan
cannot be compared to int
type value 10
.
The error "Operator < is undefined for argument type Scanner,int" means "There is no comparison operator <
to compare the value of type Scanner
to type ", not something of type
Scanner
.
It appears to be hidden with a different error, but s<100
is also not a valid expression.In your code, s
represents an array of int
types.Java arrays do not have the ability to write only the array name s
to represent the sum.If you need a total value, you need to calculate it separately.
Your code is for(inti=0;i<10;i++){...}
and you are trying to write a condition in while(...){...}
to "stop" after 10 loops.
In your code, scan
represents an instance of type Scanner
.This doesn't reflect your intentions and sometimes indicate the number of inputs.Therefore, scan
cannot be compared to int
type value 10
.
The error "Operator < is undefined for argument type Scanner,int" means "There is no comparison operator I will show you my own code that has been modified. Examples of operation/output (code shaped is output) 「 It is normal to judge that 100 is not exactly 100 when the expression exceeds 100.It is not clear whether the previous value will contain the last value that has been exceeded, but the code above will result in the "include" behavior.(If I give this assignment and don't get a circle, I'll complain that it's bad that the questions are ambiguous.) Operation/Output Example 2 (Stop due to conditions other than the question specification) For the latest Java code, as Kohei TAMURA indicated in the previous question, I think it would be better to use I've left a lot of comments in detail.I hope that you will not be able to complete the assignment report without fully understanding it, but instead move on to the next assignment, and that you will be able to increase the number of things that you can solve yourself in the next assignment.<
to compare the value of type Scanner
to type ", not something of type
Scanner
.s<100
is also not a valid expression.In your code, s
represents an array of int
types.Java arrays do not have the ability to write only the array name s
to represent the sum.If you need a total value, you need to calculate it separately.public static void main(String[]args){
// array s initialization
int[]s = new int[10];
//### have a variable representing the total value of
int total = 0;
//### have variables indicating how far someone has entered
int lastIndex=-1;
System.out.println("Enter an integer"); //#### The first time should be enough to display the plot.
Scanner scan = new Scanner (System.in); //###You do not need to create an instance of Scanner per loop.
// Accept input 10 times (=array size)
for(inti=0;i<s.length;++i){//#### When spinning the entire array, i<s.length is a common phrase.
try{
s[i] = scan.nextInt();
total+=s[i]; //### Update Total Value
//### remember how far someone added to the total
lastIndex=i;
//### Input stops when the total value exceeds 100
//### ("Once input has been made 10 times" can be accomplished with for()
if(total>100){
break;
}
} catch(Exceptione){
//### Ended even if the condition is not in question but an integer value cannot be entered
break;
}
}
scan.close();
// Output all entered numbers
for(inti=0;i<=lastIndex;++i){//###`lastIndex`The value is also valid, so `<=`
System.out.println(s[i]);
}
}
Enter an integer
10
20
30
40
5010
20
30
40
50
Enter an integer
10
20
30
end10
20
30
try-with-resources
, but I don't know the version of Java used, so it's written to work a little older Java.(It is said that an old Java programmer is used to writing.)
ints[] = new int[10];
int input = 0;
// Clear Array s
for(inti=0;i<10;i++){
s[i] = 0;
}
Scannerscan;
// Enter a value from the keyboard
for(inti=0;i<10;i++){
System.out.println("Enter an integer");
scan = new Scanner (System.in);
s[i] = scan.nextInt();
int sum = 0;
for(int j=0;j<10;j++){
sum+=s[i];
}
if(sum>100)break;
}
© 2024 OneMinuteCode. All rights reserved.