Jump to content

My Java Program Update


Guest h4344

Recommended Posts

This is how far i am with it. Would like to thank Capt.Xander and LCpl.Spartan for all their help so far with this.

 

import java.util.Scanner;

 

public class h4Math {

 

public static void main(String []args) {

 

//Created By Harry Laurent.

 

 

Scanner scan = new Scanner(System.in);

 

double answer1 = 0;

double answer2 = 0;

double answer3 = 0;

double answer4 = 0;

double answer5 = 0;

double answer6 = 0;

double answer7 = 0;

double answer8 = 0;

double answer9 = 0;

double answer10 = 0;

 

double fnum1 = 0, snum1 = 0;

double fnum2 = 0, snum2 = 0;

double fnum3 = 0, snum3 = 0;

double fnum4 = 0, snum4 = 0;

double fnum5 = 0, snum5 = 0;

double fnum6 = 0, snum6 = 0;

double fnum7 = 0, snum7 = 0;

double fnum8 = 0, snum8 = 0;

double fnum9 = 0, snum9 = 0;

double fnum10 = 0, snum10 = 0;

 

int Right = 0;

int Wrong = 0;

 

double yes = 0;

 

boolean again = true;

 

 

 

System.out.println("-------------------------");

System.out.println("Created By Harry Laurent");

System.out.println("Version 0.7");

System.out.println("-------------------------");

 

 

 

 

while(again == true) {

 

//Problem 1

System.out.println("What 2 numbers = 4?");

System.out.println("Enter First Number: ");

fnum1 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum1 = scan.nextDouble();

answer1 = fnum1 + snum1;

 

 

if(answer1 == 4) {

Right += 1;

}

 

if(answer1 != 4) {

Wrong += 1;

}

 

//Problem 2

System.out.println("What 2 numbers = 11?");

System.out.println("Enter First Number: ");

fnum2 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum2 = scan.nextDouble();

answer2 = fnum2 + snum2;

 

 

if(answer2 == 11) {

Right += 1;

}

 

if(answer2 != 11) {

Wrong += 1;

}

 

//Problem 3

System.out.println("What 2 numbers = 24?");

System.out.println("Enter First Number: ");

fnum3 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum3 = scan.nextDouble();

answer3 = fnum3 + snum3;

 

 

if(answer3 == 24) {

Right += 1;

}

 

if(answer3 != 24) {

Wrong += 1;

}

 

//Problem 4

System.out.println("What 2 numbers = 36?");

System.out.println("Enter First Number: ");

fnum4 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum4 = scan.nextDouble();

answer4 = fnum4 + snum4;

 

 

if(answer4 == 36) {

Right += 1;

}

 

if(answer4 != 36) {

Wrong += 1;

}

 

//Problem 5

System.out.println("What 2 numbers = 120?");

System.out.println("Enter First Number: ");

fnum5 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum5 = scan.nextDouble();

answer5 = fnum5 + snum5;

 

 

if(answer5 == 120) {

Right += 1;

}

 

if(answer5 != 120) {

Wrong += 1;

}

 

//Problem 6

System.out.println("What 2 numbers = 50?");

System.out.println("Enter First Number: ");

fnum6 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum6 = scan.nextDouble();

answer6 = fnum6 + snum6;

 

 

if(answer6 == 50) {

Right += 1;

}

 

if(answer6 != 50) {

Wrong += 1;

}

 

//Problem 7

System.out.println("What 2 numbers = 1200?");

System.out.println("Enter First Number: ");

fnum7 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum7 = scan.nextDouble();

answer7 = fnum7 + snum7;

 

 

if(answer7 == 1200) {

Right += 1;

}

 

if(answer7 != 1200) {

Wrong += 1;

}

 

//Problem 8

System.out.println("What 2 numbers = 60?");

System.out.println("Enter First Number: ");

fnum8 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum8 = scan.nextDouble();

answer8 = fnum8 + snum8;

 

 

if(answer8 == 60) {

Right += 1;

}

 

if(answer8 != 60) {

Wrong += 1;

}

 

//Problem 9

System.out.println("What 2 numbers = 600?");

System.out.println("Enter First Number: ");

fnum9 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum9 = scan.nextDouble();

answer9 = fnum9 + snum9;

 

 

if(answer9 == 600) {

Right += 1;

}

 

if(answer9 != 600) {

Wrong += 1;

}

 

//Problem 10

System.out.println("What 2 numbers = 56.04?");

System.out.println("Enter First Number: ");

fnum10 = scan.nextDouble();

System.out.println("Enter Second Number: ");

snum10 = scan.nextDouble();

answer10 = fnum10 + snum10;

 

 

if(answer10 == 56.04) {

Right += 1;

}

 

if(answer10 != 56.04) {

Wrong += 1;

}

 

 

//If statements for if they passed or not.

 

if(Right > Wrong) {

System.out.println(Right + " Right," + Wrong + " Wrong" + ", Congrats You Passed!");

}

 

if(Wrong > Right) {

System.out.println(Right + " Right," + Wrong + " Wrong" + ", Sorry You Did Not Pass.");

}

 

if(Right == Wrong) {

System.out.println(Right + " Right," + Wrong + " Wrong" + ", Same Amount Right As Wrong.");

 

 

}

 

// Do they want to retake?

 

System.out.println("Do you want to retake the test? (1 for yes or 0 for no)");

yes = scan.nextDouble();

 

if(yes == 1) {

again = true;

}

 

if(yes == 0) {

again = false;

}

 

 

if(again == false) {

System.out.println("Done!");

 

}

}

 

}

 

}

Link to comment
Share on other sites

Guest Ret.Maj.Xander=US=

few suggestions. Your fnum and answer variables can but put into an array which would clean up the code. Also you need to look into using Else If not just If (someting){//do something} If(something else){//something else}

 

You should also look into global variables for the Right and Wrong variables.

 

Last suggestion is breaking that code out of the Main function and use smaller sub functions to do most of the work then calling them in the main function.

 

Edit: After looking at it more, should there be a different variable for each answer? Couldn't you just use the same variable for each question? Just set it to 0 before you do anything to it in each question.

 

Know that the goal in programming is to maximize functionality and minimize un-needed code.

Link to comment
Share on other sites

I could definantly shorten the code a bit. Atm though since im still learning i am not familiar with all options that an experienced eye might catch.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...