Question:

Can someone fix this Java program?

by Guest66197  |  earlier

0 LIKES UnLike

This is a Java program I wrote. All it is supposed to do is add a and b. Then it displays the total. But instead of displaying the total, it displays the word "total" Why. I will be giving 10 points if you tell me how to do that too.

import java.util.Scanner;

class Add {

public static void main(String args[]){

Scanner myScanner = new Scanner(System.in);

double a;

double b;

double total;

System.out.print("a= ");

a = myScanner.nextDouble();

System.out.print("b= ");

b = myScanner.nextDouble();

System.out.println("total");

total = a + b;

}

}

 Tags:

   Report

8 ANSWERS


  1. You are missing a line of code. You have displayed "a=", "b=", and "total", and you have calculated total just fine. Now add a line for System.out.println(total);

    This will display the value of the total variable.

    10 pts plz. :)

    Everyone is telling you that you have messed up on the quotes around the word total. This is not true. You have simply printed the word total above where the value should go. After the line where you set the value of total to be the sum of A and B, simply print the value of total. This is a more professional look.

    Think outside the box here people. A person can label their values on the output screen.

    That is all, and I still am missing 10 pts. :)


  2. awwww... that is so cute! Made me smile

    ok to fix it do this to the last 2 lines....

    1. swap them around.

    2. remove the " quotes around the word total

    so that it looks like this

    total = a + b;

    System.out.println(total);

  3. import java.util.Scanner;

    class Add {

    public static void main(String args[]){

    Scanner myScanner = new Scanner(System.in);

    double a;

    double b;

    double total;

    System.out.print("a= ");

    a = myScanner.nextDouble();

    System.out.print("b= ");

    b = myScanner.nextDouble();

    System.out.println(total);

    total = a + b;

    }

    }

    Try this.

  4. Try asking a computer answer website

  5. Its because you put total in quotes

    I dont know Java I know C#, but their similar and in C# quotations mean its a string meaning print that word without quotes its a variables value which is what u want.

    Tell me if it works without quotes:)

  6. remove quotation marks from "Total" in println statement near the end

  7. You forgot to print total AFTER adding a + b.  You also put total in quotes so it makes it a string and therefor printing the word total.

    Do this:

    System.out.println(total);

    Remember to do without the quotes since you want to print the double member variable called total after adding.

    If you want to display the values entered for a and b, then do something like:

    a = myScanner.nextDouble();

    System.out.println("a= " + a);

  8. You have quotes around the word total, so it will be quoted exactly in the output.  Delete the quotes, and you will output the value of the variable total.  You will also need to swap your last two lines of code.  First compute the variable total, then output it.

Question Stats

Latest activity: earlier.
This question has 8 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.