java4:选择语句(selection),逻辑运算,循环(loop)

来源:互联网 发布:男士滑雪帽 知乎 编辑:程序博客网 时间:2024/05/24 15:38

直接用例子来practice

example one:  practice first-grader  subtraction 

Suppose you want to develop a program for a first-grader to practice subtraction. The program
randomly generates two single-digit integers, number1 and number2, with number1 > =
number2 and displays to the student a question such as “What is ” After the student
enters the answer, the program displays a message indicating whether it is correct.
9 - 2?

初级减法运算,利用随机工具产生两个随机数字,num1 ,num2 ,num1>=num2 然后显示 num1-num2=? 让用户输入一个答案,显示是否答对

import java.util.Scanner;public class Subtraction{public static void main(String[] args){int num1=(int)(Math.random()*10);int num2=(int)(Math.random()*10);if(num2>num1){int temp=num1;num1=num2;num2=temp;}Scanner input=new Scanner(System.in);System.out.println(num1+" - "+num2+" = ?");System.out.print("input your answer:");int in=input.nextInt();if(in == num1-num2){System.out.println("your are right,"+num1+"-"+num2+"="+in);}elseSystem.out.println("you are wrong,"+num1+"-"+num2+"="+(num1-num2));}}


这里注意两个点:1)因为利用Math.random产生了[0,1)之间的一个double数字,所以我们需要使用显式类型转换, 2)注意输入的要求num1>num2 但我们不能决定随机函数先产生大的再产生小的,所以产生后别忘了加一个简单的判断交换。

example two:Lottery
Suppose you want to develop a program to play lottery. The program randomly generates a
lottery of a two-digit number, prompts the user to enter a two-digit number, and determines
whether the user wins according to the following rule:
1. If the user input matches the lottery in exact order, the award is $10,000.
2. If all the digits in the user input match all the digits in the lottery, the award is $3,000.
3. If one digit in the user input matches a digit in the lottery, the award is $1,000

import java.util.Scanner;public class Lottery{public static void main(String[] args){int lot=(int)(Math.random()*100);Scanner input =new Scanner(System.in);int gue;do{System.out.print("input your guess:");gue=input.nextInt();}while(gue<10 || gue>100);if(lot==gue){System.out.println("you match all,you win $10,000");System.exit(0);}int lot1=lot/10;int lot2=lot%10;int gue1=gue/10;int gue2=gue%10;if(lot1==gue2 && lot2==gue1){System.out.println("you match the digital but sorry for the order,you win $3,000,the lot is" + lot);//System.exit(0);}else if(lot1==gue1 || lot1==gue1 || lot2==gue1 || lot2 == gue2){System.out.println("you match one digital,you win $1000,the lot is "+lot);//System.exit(0);}elseSystem.out.println("sorry for that you lose, the lottery is "+lot);}}
运行结果:

其中有点奇怪,就是怎么产生的随机数会有个2的呢?? 我明明ramdon后是乘以100了,那么除了可能产生0(这里其实应该特殊处理才严谨点,下回改进),不应该会有产生2呀??
example three: 矩阵形式打印九九乘法表

public class MultiTable{public static void main(String [] args){int i,j;System.out.print("    ");for(i=1;i<10;i++){System.out.printf("%4d",i);}System.out.println();System.out.println("----------------------------------------------------------");for(i=1;i<10;i++){System.out.printf("%d | ",i);for(j=1;j<10;j++){System.out.printf("%4d",i*j);}System.out.println();}}}


example four: display primenumber

The problem is to display the first 50 prime numbers in five lines, each of which contains
n numbers. The problem can be broken into the following tasks:
■ Determine whether a given number is prime.
■ For number 2, 3, 4, 5, 6, test whether it is prime.
■ Count the prime numbers.
■ Print each prime number, and print ten numbers per line.

public class primeNumber{public static void main(String [] args){int num=1;int count=0;System.out.printf("%4d",2);count++;while(count<50){num+=2;if(ifPrime(num)) {System.out.printf("%4d",num);count++;if(count%10==0) System.out.println();}}}public static boolean ifPrime(int n){if(n%2==0) return false;int h=(int)java.lang.Math.sqrt(n);for(int i=3;i<=h;i+=2){if(n%i==0) return false;}return true;}}


这里注意里边多了一个public static boolean ifPrime 用来判断是给定的一个数是否是素数,这里是定义一个类方法,这到后面会见到,这里先知道有这个用法。


0 0
原创粉丝点击