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!"); } } }