Adapted from 4.3: #4 - In a game program, the scores of players A and B are stored in two int variables scoreA and scoreB. Input these two scores from the user with a Scanner. Then, if the player with the larger score wins, use if statements (and else if/else as necessary) to print out "A won", "B won", or "Game tied".
top of page
bottom of page
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Player 1 score"); int A= sc.nextInt(); System.out.println("Player 2 score"); int B= sc.nextInt(); if(A>B) { System.out.println("Player 1 won!"); } else if(B>A) { System.out.println("Player 2 won!"); } else { System.out.println("It is a tied game!"); } } }
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner x = new Scanner(System.in); int ScoreA = x.nextInt(); int ScoreB = x.nextInt(); if (ScoreA > ScoreB) { System.out.println("A won"); } else if (ScoreB > ScoreA) { System.out.println("B won"); } else { System.out.println("Game Tied"); } } }
Here's My code:
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Player 1 Score: "); int scoreA = sc.nextInt(); System.out.println("Player 2 Score: "); int scoreB = sc.nextInt(); if (scoreB > scoreA) { System.out.println("B Won"); } else if (scoreA > scoreB) { System.out.println("A Won"); } else { System.out.println("Tie"); } } }