Welcome to JAVA!(第3章课后习题)

3.4 Write a program that generatestwo intergers under 100 and prompts theuser to enter the sumof thesetwo integers.The program then reports true if the answer is correct,flase otherwise.The program is similar toList3.1

import java.util.Scanner;class Main {public static void main(String[] args) {int number1= (int) (System.currentTimeMillis()%100);int number2= (int) (System.currentTimeMillis()*7%100);Scanner input=new Scanner(System.in);System.out.println("what is " + number1 + "+" + number2 + "?");System.out.print("your answer: ");int answer = input.nextInt();System.out.println( number1 + "+" + number2 + " = " + answer + " is " + (number1 + number2 == answer));}}/* output:what is 90+30?your answer: 12090+30 = 120 is true*///~

import java.util.Scanner;class Main {public static void main(String[] args) {int number1= (int) (Math.random()*100);int number2= (int) (Math.random()*100);Scanner input=new Scanner(System.in);System.out.println("what is " + number1 + "+" + number2 + "?");System.out.print("your answer: ");int answer = input.nextInt();System.out.println( number1 + "+" + number2 + " = " + answer + " is " + (number1 + number2 == answer));}}/* output:what is 95+83?your answer: 17895+83 = 178 is truewhat is 35+37?your answer: 6635+37 = 66 is false*///~

3.22 Write a program that prompts the user to enter a point(x,y) and checks whether the point is within the circle centered at (0,0) with radius 10.

import java.util.Scanner;class Main {public static void main(String[] args) {Scanner input=new Scanner(System.in);System.out.print("Enter a point with two coordinates:");double x=input.nextDouble(),y=input.nextDouble();double length,radius=10;length=Math.pow(x*x+y*y, 0.5);if (length <= radius)System.out.print("Point (" + x + ", " + y +") is in the Ciccle");elseSystem.out.print("Point (" + x + ", " + y +") is not in the Ciccle");}}/* output:Enter a point with two coordinates:4 5Point (4.0, 5.0) is in the CiccleEnter a point with two coordinates:8 9Point (8.0, 9.0) is not in the Ciccle*///~

3.24 Write a program that simulates picking a card from a deck of 52 cards,your program should display the rank(Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King) and suit (Clubs,Diamonds,Hearts,Spaders)of the card.

(1)case语句

import java.util.Scanner;class Main {public static void main(String[] args) {int number1= (int) (Math.random()*13);int number2= (int) (Math.random()*4);System.out.print("The card your picked is ");switch(number1){case 0:System.out.print("Ace of ");break;case 1:System.out.print("2 of ");break;case 2:System.out.print("3 of ");break;case 3:System.out.print("4 of ");break;case 4:System.out.print("5 of ");break;case 5:System.out.print("6 of ");break;case 6:System.out.print("7 of ");break;case 7:System.out.print("8 of ");break;case 8:System.out.print("9 of ");break;case 9:System.out.print("10 of ");break;case 10:System.out.print("Jack of ");break;case 11:System.out.print("Queen of ");break;case 12:System.out.print("King of ");break;}switch(number2){case 0:System.out.print("Clubs");break;case 1:System.out.print("Diamonds");break;case 2:System.out.print("Hearts");break;case 3:System.out.print("Spades");break;}}}/* output:The card your picked is 10 of ClubsThe card your picked is Jack of Diamonds*///~

(2)数组

import java.util.Scanner;class Main {public static void main(String[] args) {int number1= (int) (Math.random()*13);int number2= (int) (Math.random()*4);String[] num1={"Ace","1","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};String[] num2={"Clubs","Diamonds","Hearts","Spades"};System.out.print("The card your picked is "+ num1[number1] + " of " + num2[number2]);}}/* output:The card your picked is 9 of ClubsThe card your picked is 8 of Spades*///~

学习心得:

学会了两种JAVA取随机数的方法以及Selections的各种内容,,在IF-ELSE、switch等等上和C++并没有多大的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。

读书须用意,一字值千金。

Welcome to JAVA!(第3章课后习题)

相关文章:

你感兴趣的文章:

标签云: