External Exam Download Resources Web Applications Games Recycle Bin

simple input


//save as simpleInput.java
import java.util.Scanner;

public class simpleInput {
    public static void main(String[] args) { 
      Scanner myInput = new Scanner(System.in);
      String mySentence = "";
      mySentence = myInput.nextLine();
      System.out.println("your entered: " + mySentence);
    }
}

find max
//save as findMax.java
import java.util.Scanner;

public class findMax {
    public static void main(String[] args) { 
       int numberOne;
       int numberTwo;
       int max;
       String userInput;
       Scanner myScanner = new Scanner(System.in);
       
       System.out.println("Enter your first number:");
       userInput = myScanner.nextLine();
       numberOne = Integer.valueOf(userInput);
       
       System.out.println("Enter your second number:");
       userInput = myScanner.nextLine();
       numberTwo = Integer.valueOf(userInput);
       
       if(numberOne > numberTwo)
         {
           max = numberOne;
         }
       else
         {
           max = numberTwo;
         }
       
       System.out.println(String.valueOf(max));
    }
}

rock paper scissors
//save as rps.java
import java.util.*;
public class rps {
  public static void main(String[] args) { 
    Boolean userWinning = false;
    String computerPick = "";
    String userPick = "";
    
    Random seed = new Random();
    int unknownNumber = seed.nextInt(3) + 1;  
    if(unknownNumber==1) computerPick = "Rock";
    if(unknownNumber==2) computerPick = "Paper";
    if(unknownNumber==3) computerPick = "Scissors";
    
    Scanner myInput = new Scanner(System.in);
    userPick = myInput.nextLine().trim();
    System.out.println("You chose: " + userPick);
    System.out.println("Computer chose: " + computerPick);
    
    if(computerPick.equals(userPick)){ System.out.println("Draw"); }
    if(computerPick.equals("Rock") && userPick.equals("Paper")){ userWinning = true; }
    if(computerPick.equals("Scissors") && userPick.equals("Rock")){ userWinning = true; }
    if(computerPick.equals("Paper") && userPick.equals("Scissors")){ userWinning = true; }
    
    if(userWinning)  
        { System.out.println("Winner!"); userWinning = false; }
        else
        { System.out.println("You didnt win!"); }

  }
}