简体   繁体   中英

How do I get my java program to output the answer?

I'm new to java and my class has a homework assignment that I need a little help on. I finished everything except for the output, I can't seem to get the program to produce the answer.

    import java.util.Scanner;
    public class HomeworkScanner
   {

     public static void main(String args[])
     {

            int x, y, z, a, result;
            Scanner Keyboard;
            Keyboard = new Scanner(System.in);
            System.out.println("a = ( x + y ) * z");
            System.out.println("Enter first number for x:");
            x = Keyboard.nextInt(); //
            System.out.println("Enter second number for y:");
            y = Keyboard.nextInt(); //
            System.out.println("Enter third number for z:");
            z = Keyboard.nextInt(); //
            result=a=( x + y ) * z;
         }
    }

I'm assuming you want the format "Result = a" which would be

System.out.println("Result = " + result);

Or you could use a since you set them to the same value

import java.util.Scanner;
public class HomeworkScanner
{

 public static void main(String args[])
 {

        int x, y, z, a, result;
        Scanner Keyboard;
        Keyboard = new Scanner(System.in);
        System.out.println("a = ( x + y ) * z");
        System.out.println("Enter first number for x:");
        x = Keyboard.nextInt(); //
        System.out.println("Enter second number for y:");
        y = Keyboard.nextInt(); //
        System.out.println("Enter third number for z:");
        z = Keyboard.nextInt(); //
        result=a=( x + y ) * z;
        System.out.println("result is :"+result);
     }
}

You can just use System.out.println("Result: "+result); to print your result in the console.

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