简体   繁体   中英

I keep getting illegal start of expression error codes and ';' expected error codes and I cannot figure out why

I am trying to solve the MixedFractions problem on Kattis. I know the basic structure that the code has to be in, but I cannot seem to get it to function the way I need it to. I want the While Loop to read in two integers and, while they are not both zero, carry out what I have put below it. When I compile it however I keep getting the same errors.

import java.util.Scanner;
class MixedFractions2{
        public static void main(String args[]){
                Scanner scan = new Scanner(System.in);
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                        int give = (num % den);
                        if (num % den == 0){
                                System.out.println(num/den + " " + 0 + " " + "/" + " " + den);
                        } else {
                                System.out.println(num/den + " " + give + " " + "/" + " " + den);
                        }
                }
        }
}

The errors that I keep getting look like so;

MixedFractions2.java:5: error: '.class' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                           ^
MixedFractions2.java:5: error: illegal start of expression
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                               ^
MixedFractions2.java:5: error: ';' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                ^
MixedFractions2.java:5: error: illegal start of expression
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                     ^
MixedFractions2.java:5: error: ';' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                             ^
MixedFractions2.java:5: error: ';' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                                                                     ^

I have tried putting parenthesis in the While loop to separate out the two scanners but that does not seem to be the problem as far as I can tell. I am not sure what else I could be doing wrong.

I am trying to solve the MixedFractions problem on Kattis. I know the basic structure that the code has to be in, but I cannot seem to get it to function the way I need it to. I want the While Loop to read in two integers and, while they are not both zero, carry out what I have put below it. When I compile it however I keep getting the same errors.

import java.util.Scanner;
class MixedFractions2{
        public static void main(String args[]){
                Scanner scan = new Scanner(System.in);
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                        int give = (num % den);
                        if (num % den == 0){
                                System.out.println(num/den + " " + 0 + " " + "/" + " " + den);
                        } else {
                                System.out.println(num/den + " " + give + " " + "/" + " " + den);
                        }
                }
        }
}

The errors that I keep getting look like so;

MixedFractions2.java:5: error: '.class' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                           ^
MixedFractions2.java:5: error: illegal start of expression
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                               ^
MixedFractions2.java:5: error: ';' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                ^
MixedFractions2.java:5: error: illegal start of expression
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                     ^
MixedFractions2.java:5: error: ';' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                             ^
MixedFractions2.java:5: error: ';' expected
                while (int num = scan.nextInt() != 0 && int den = scan.nextInt() != 0){
                                                                                     ^

I have tried putting parenthesis in the While loop to separate out the two scanners but that does not seem to be the problem as far as I can tell. I am not sure what else I could be doing wrong.

The following code produces no compiler errors.

import java.util.Scanner;

public class MixedFractions2 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num;
        int den;
        while ((num = scan.nextInt()) != 0 && (den = scan.nextInt()) != 0) {
            int give = (num % den);
            if (num % den == 0) {
                System.out.println(num / den + " " + 0 + " " + "/" + " " + den);
            }
            else {
                System.out.println(num / den + " " + give + " " + "/" + " " + den);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM