Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

jump to level 1 define a method checkvals() with a scanner parameter th…

Question

jump to level 1
define a method checkvals() with a scanner parameter that reads integers from input until 0 is read. checkvals() should return true if none of the integers read before 0 are between 90 and 100, inclusive. otherwise, checkvals() should return false.
ex: if the input is 5 89 101 0, then the output is:
all allowed values
1 import java.util.scanner;
2
3 public class valuechecker {
4
5 / your code goes here /
6
7 public static void main(string args) {
8 scanner scnr = new scanner(system.in);
9 boolean allallowed;
10
11 allallowed = checkvals(scnr);
12
13 if (allallowed) {
14 system.out.println(\all allowed values\);
15 }

Explanation:

Step1: Define the method

public static boolean checkVals(Scanner scnr) {

Step2: Initialize variables

    int num;
    boolean foundInvalid = false;

Step3: Read integers

    while ((num = scnr.nextInt()) != 0) {

Step4: Check range

        if (num >= 90 && num <= 100) {
            foundInvalid = true;
        }
    }

Step5: Return result

    return !foundInvalid;
}

Answer:

import java.util.Scanner;

public class ValueChecker {
    public static boolean checkVals(Scanner scnr) {
        int num;
        boolean foundInvalid = false;
        while ((num = scnr.nextInt()) != 0) {
            if (num >= 90 && num <= 100) {
                foundInvalid = true;
            }
        }
        return !foundInvalid;
    }

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        boolean allAllowed;

        allAllowed = checkVals(scnr);

        if (allAllowed) {
            System.out.println("All allowed values");
        } else {
            System.out.println("Some values not allowed");
        }
    }
}