1. 程式人生 > >10.9

10.9

mem bit ssd erp shee log ilog next 1-n

3-1

import java.util.Scanner;

public class AdditionQuiz
{
public static void main(String[] args)
{
int number1=(int)(System.currentTimeMillis()%10);
int number2=(int)(System.currentTimeMillis()/7%10);
Scanner input=new Scanner(System.in);

System.out.print(
"What is "+number1+"+"+number2+"?");
int answer = input.nextInt();
System.out.println(
number1+"+"+number2+"="+answer+" is "+(number1+number2==answer));
}
}

技術分享

3-2

import java.util.Scanner;

public class SimpleIfDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer:");
int number = input.nextInt();
if (number % 5 == 0)
System.out.println("HiFive");
if(number % 2 == 0)
System.out.println("HiEven");
}
}

技術分享

3-3

import java.util.Scanner;

public class SubtractionQuiz
{
public static void main(String[] args)
{
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

if (number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}
System.out.print
("What is " + number1 + "-" + number2 + "?");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
if (number1 - number2 == answer)
System.out.println("You are correct!");
else{
System.out.println("Your answer is wrong.");
System.out.println(number1 + "-" + number2 + "should be " + (number1-number2));
}
}
}
技術分享

3-4

import java.util.Scanner;

public class ComputeAndInterpretBMI
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter weight in kilograms:");
double weight = input.nextDouble();

System.out.print("Enter height in meters:");
double height = input.nextDouble();

double bmi = weight/(height * height);

System.out.println("BMI is " + bmi);
if (bmi < 18.5)
System.out.println("Underweight");
else if (bmi < 25)
System.out.println("Normal");
else if (bmi < 30)
System.out.println("Overweight");
else
System.out.println("Obese");
}
}

技術分享

3-6

import java.util.Scanner;

public class TestBooleanOperators
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter an integer:");
int number = input.nextInt();
if (number % 2 == 0 && number % 3 ==0)
System.out.println(number + " is divisible by 2 and 3.");
if (number % 2 == 0 || number % 3 ==0)
System.out.println(number + " is divisible by 2 or 3.");
if (number % 2 == 0 ^ number % 3 ==0)
System.out.println(number + " is divisible by 2 or 3, but not both.");
}
}

技術分享

3-7

import java.util.Scanner;

public class LeapYear
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a year:");
int year = input.nextInt();
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
System.out.println(year + " is a leap year?" + isLeapYear);
}
}

技術分享

3-8

import java.util.Scanner;

public class Lottery
{
public static void main(String[] args)
{
int lottery = (int)(Math.random() * 100);
Scanner input = new Scanner(System.in);

System.out.print("Enter your lottery pick (two digits):");
int guess = input.nextInt();
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2)
System.out.println("Match all digits: you win $3,000");
else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match");
}
}

技術分享

3-9

import java.util.Scanner;

public class ChineseZodiac
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter a year:");
int year = input.nextInt();
switch (year % 12)
{
case 0: System.out.println("monkey");break;
case 1: System.out.println("rooster");break;
case 2: System.out.println("dog");break;
case 3: System.out.println("pig");break;
case 4: System.out.println("rat");break;
case 5: System.out.println("ox");break;
case 6: System.out.println("tiger");break;
case 7: System.out.println("rabbit");break;
case 8: System.out.println("dragon");break;
case 9: System.out.println("snake");break;
case 10: System.out.println("horse");break;
case 11: System.out.println("sheep");break;

}
}
}
技術分享

10.9