import java.util.Scanner;

public class Week5LabQ1 {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter two integer number: ");
        int firstNum = s.nextInt(),secondNum= s.nextInt();
        System.out.print("Enter the operand: ");
        char charactor = s.next().charAt(0);

        if (charactor == '/'){
            System.out.println(firstNum+" "+charactor+" "+secondNum+" = "+(firstNum/secondNum));
        }else if (charactor == '+'){
            System.out.println(firstNum+" "+charactor+" "+secondNum+" = "+(firstNum+secondNum));
        }else if (charactor == '-'){
            System.out.println(firstNum+" "+charactor+" "+secondNum+" = "+(firstNum-secondNum));
        }else if (charactor == '*'){
            System.out.println(firstNum+" "+charactor+" "+secondNum+" = "+(firstNum*secondNum));
        }else if (charactor == '%'){
            System.out.println(firstNum+" "+charactor+" "+secondNum+" = "+(firstNum%secondNum));
        }else{
            System.out.println("Your operand is invalid.");
        }
    }
}

import java.util.Scanner;

public class Week5LabQ2 {
    public static void main(String[] args) {
        String[] english = new String[]{"zero", "one", "two", "three", "four", "five"};
        System.out.print("Enter an integer from 0 to 5 : ");
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        try {
            System.out.print(i + " is " + english[i] + ".");
        }catch(Exception e){
            System.out.println("Invalid Number");
        }
    }
}

import java.util.Scanner;

public class Week5LabQ3 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the volume(Integer) : ");
        int volume = s.nextInt();
        double commission;
        if (volume > 1000){
            commission = 0.125*(volume-1000) + 0.1*500 + 0.075*400 + 0.05*100;
        }else if (volume > 500){
            commission = 0.1*(volume-500) + 0.075*400 + 0.05*100;
        }else if (volume > 100){
            commission = 0.075*(volume-100) + 0.05*100;
        }else if (volume < 100 && volume >= 0){
            commission = 0.05*volume;
        }else{
            System.out.println("Wrong number");
            commission = -1;
        }
        System.out.printf("%.2f",commission);
    }
}

import java.util.Random;

public class Week5LabQ4 {
    public static void main(String[] args) {
        Random r = new Random();
        final int MAX = 6;
        int player1 = r.nextInt(MAX) + 1;
        int player2 = r.nextInt(MAX) + 1;
        System.out.println("Player 1 :" + player1 + "\nPlayer 2 : " + player2);
        String winner = "Player 1 wins";
        int score = player1;
        if (player1 < player2){
            winner = "Player 2 wins";
            score = player2;
        }
        if (player1 == player2){
            winner = "Round Draw";
        }
        System.out.println( winner + " with " + score );
    }
}


这题更完整的程序在这里

import java.util.Scanner;

public class Week5LabQ5 {
    public static void main(String[] args) {
        double a,b,c,d,e,f,x,y;
        Scanner s = new Scanner(System.in);
        System.out.print("ax + by =e, cx + dy = f\n");
        System.out.print("Enter a,b,e : ");
        a = s.nextDouble();
        b = s.nextDouble();
        e = s.nextDouble();
        System.out.println(a+"x+"+b+"y="+e);
        System.out.print("Enter c,d,f : ");
        c = s.nextDouble();
        d = s.nextDouble();
        f = s.nextDouble();
        System.out.println(c+"x+"+d+"y="+f);
        x = (e*d-b*f) / (a*d-b*c);
        y = (a*f-e*c) / (a*d-b*c);
        if (a*d-b*c == 0 ){
            System.out.println("The equation has no solution.");
        }else{
            System.out.println("x="+x+",y="+y);
        }
    }
}

import java.util.Scanner;

public class Week5LabQ6 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter x of the coordinate point(x,y) : ");
        int x = s.nextInt();
        System.out.print("Enter y of the coordinate point(x,y) : ");
        int y = s.nextInt();
        System.out.print("Enter the radius of the circle : ");
        double r = s.nextDouble();

        if (x*x + y*y <= r*r){
            System.out.println("The point is inside the circle.");
        }else{
            System.out.println("The point is outside the circle.");
        }


    }
}

发表评论