a program that repeatedly inputs values in Java and stops input when the total value exceeds 100 or when input is made 10 times and outputs the previous value

Asked 1 years ago, Updated 1 years ago, 29 views

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
}

java

2022-09-30 21:26

2 Answers

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 < 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.

I will show you my own code that has been modified.

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]);
    }
}

Examples of operation/output (code shaped is output)

Enter an integer
10
20
30
40
50

10
20
30
40
50

「 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)

Enter an integer
10
20
30
end

10
20
30

For the latest Java code, as Kohei TAMURA indicated in the previous question, I think it would be better to use 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.)

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.


2022-09-30 21:26

 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;
}


2022-09-30 21:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.