1.简单的使用while loop。
从下面的code中找到使用while loop的sum of the first N numbers.在每一个重复的loop里根据变量sum和i放一个print语句在loop.int sum = 0;
final int numTimes = 3;
int i = 1;
while ( i <= numTimes ){
sum += i;
i++;
}
System.out.print(“The sum of the first ”+numTimes);
System.out.print(“numbers is: ”+sum);改变loop到sum的下一个N值from 5. 例子5+6+7....[/color]
改变code到print out the numbers from N down to 0.2.简单的星号(*)printing one
在主要的类函数里写一个分段码要求一个整数from用户 and print that number of 星号(*), characters to the screen.[color=#FF0000]例子N=3,print***3.简单的星号(*)printing two
在主要的类函数里写一个分段码要求两个整数和output an n*m(n equals to row no, and m equals to column no)网格星号字符.
例子 n=4,m=5 *****
* *
* *
*****(1).你可以改一下你刚刚写的code去print一个星号字符的直角三角形代替一整个的n*n网格吗?
例子N=4 *
* *
* * *
* * * *
4.什么会是下面的分段码中的output?用netbeans查看之前记录下答案i. for(int i = 20; i > 2; i = i / 2)
System.out.print("i= "+i);ii. m = 10;
do
{
System.out.print("m= "+m);
m=m-3;
} while(m > 0);iii for(int speed = 13; speed > 0; speed -= 3)
System.out.print("Speed is "+speed);iv. for(int speed = 13; speed != 0; speed -= 3)
System.out.print("Speed is "+speed);5.改正loop中的错误找出下列分段码中的错误。
int n = 2, m = 2;
int sum = 0;for(int i = 0; i < n; i++;)
{
System.out.print("loop " + i);
for(int j == 0; j < m; i++);
{
sum += i * j;
}
System.out.println(“Sum= “ + sum);6.loop one
写一个分段码计算出第一个整数n的平方和(例子 1*1+2*2+...+n*n)7.loop two
写一个分段码print一个倒数计秒from 100 to 0 to the screen in increments of 5。8.在input中使用loop.
写一个分段码允许用户enter values和print out一些正数和负数.9.使用loop让input生效.
写一个while loop在数字1~100之间提示用户,连续提示到一个有效的入口完成。10.使用loop计算兀
一个计算兀的公式: 兀/2=2/1×2/3×4/3×4/5×6/5×6/7×8/7×8/9... (例子n=8)写一个程序可以在一个整数n中读出来,然后用第一个n的乘积公式output出兀的值提示:如果n=20,那么程序应该output出的值是3.067703806643497.(不是非常准确)11.用loop计算出斐波纳契数列.(Fibonacci sequence=斐波纳契数列是什么啊?=。=)
写一个分段码给出一个数字n,显示出斐波纳契数列的nth term,nth term是0,1,1,2,3,5,8,13......举个例子4th term应该是2. 0 if n=0;
Fn={ 1 if n=1;
Fn-1+Fn-2 if n>1. __________________________________________
| F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 |
___________________________________________
| 0 1 1 2 3 5 8 13 21 34|
____________________________________________
12.用loop计算出因数写一个while loop计算出n的因数
n!是定义为(0!=1):n!=n*(n-1)*(n-2)*(n-3)*..2*1的正整数
例子 4!=4*3*2*1=24
非常急,请各位XDJM帮帮小妹.谢谢
下面附上题目的英文原文1. Simple using while loop
The following code finds the sum of the first N numbers using a while loop. Put a print statement in the loop and follow the two variables (sum and i), during each iteration of the loop.
int sum = 0;
final int numTimes = 3;
int i = 1;
while ( i <= numTimes ){
sum += i;
i++;
}
System.out.print(“The sum of the first ”+numTimes);
System.out.print(“numbers is: ”+sum);
Alter the loop to sum the next N values from 5. i.e. 5+6+7+….
Alter the code to print out the numbers from N down to zero.
————————————————————————————————————————
2. Simple asterisk printing one
Write a code fragment in the main method to request an integer from the user and print that number of asterisk, (*), characters to the screen. E.g. N=3, print ***
——————————————————————————————————————————————
3. Simple asterisk printing two
Write a code fragment in the main method to request two integers and output an n*m (n equals to row no, and m equals to column no) grid of asterisk characters.
e.g. n = 4, m =5 *****
* *
* *
*****Can you change the code you have just written to print a right angle triangle of asterisk characters instead of a full n*n grid.
e.g. N = 4 *
* *
* * *
* * * *———————————————————————————————————————————————————————4.Simple loops
What will be the output of the following code fragments? Work it out and record your answer before checking in NetBeans.
i. for(int i = 20; i > 2; i = i / 2)
System.out.print("i= "+i);
ii. m = 10;
do
{
System.out.print("m= "+m);
m=m-3;
} while(m > 0);
iii for(int speed = 13; speed > 0; speed -= 3)
System.out.print("Speed is "+speed);
iv. for(int speed = 13; speed != 0; speed -= 3)
System.out.print("Speed is "+speed);
——————————————————————————————————————————————
5. Correct the errors in the loops
Spot the errors in the following code fragment. It is important that you can do this without the compiler, record and check your answers in the NetBeans afterwards.
int n = 2, m = 2;
int sum = 0;
for(int i = 0; i < n; i++;)
{
System.out.print("loop " + i);
for(int j == 0; j < m; i++);
{
sum += i * j;
}
System.out.println(“Sum= “ + sum);
————————————————————————————————————————————————————6. Simple for loop one
Write and test a code fragment to calculate the sum of the squares of the first n integers (i.e. 1*1 + 2*2 + ... +n*n) using a “for” loop
——————————————————————————————————————————
7. Simple for loop two
Write a code fragment that prints a countdown from 100 to 0 to the screen in increments of 5 using a “for” loop.
————————————————————————————————————————
8. Using loop in the input
Write a code fragment that allows the user to enter values and prints out the number of positive and the number of negative values entered.
————————————————————————————————————————————————————
9. Using loop to validate the input
Write a while loop that prompts the user for a number between 1 and 100 and continues to prompt the user until a valid entry is made.——————————————————————————————————————————————————10. Using loop to calculate 兀
A formula for calculating 兀 is 兀/2=2/1×2/3×4/3×4/5×6/5×6/7×8/7×8/9... (e.g. n = 8)Write a program that reads in an integer n and then outputs the value of 兀that is obtained by using the first n products of the formula
Check: If n is 20 the program should output the value 3.067703806643497. (Note: it is not very accurate!)
———————————————————————————————————————————————————————11. Using loop to calculate Fibonacci sequence
Write a code fragment that given a number n displays the nth term of the Fibonacci sequence, which is 0, 1, 1, 2, 3, 5, 8, 13….. For example the 4th term would be 2. 0 if n=0;
Fn={ 1 if n=1;
Fn-1+Fn-2 if n>1. __________________________________________
| F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 |
___________________________________________
| 0 1 1 2 3 5 8 13 21 34|
____________________________________________——————————————————————————————————12. Using loop to calculate factorial
Write a while loop to calculate n factorial.
n! is defined for a positive integer n as (0! =1): n! = n*(n-1)*(n-2)*(n-3)*…2*1
e.g. 4! = 4*3*2*1 =24
END
从下面的code中找到使用while loop的sum of the first N numbers.在每一个重复的loop里根据变量sum和i放一个print语句在loop.int sum = 0;
final int numTimes = 3;
int i = 1;
while ( i <= numTimes ){
sum += i;
i++;
}
System.out.print(“The sum of the first ”+numTimes);
System.out.print(“numbers is: ”+sum);改变loop到sum的下一个N值from 5. 例子5+6+7....[/color]
改变code到print out the numbers from N down to 0.2.简单的星号(*)printing one
在主要的类函数里写一个分段码要求一个整数from用户 and print that number of 星号(*), characters to the screen.[color=#FF0000]例子N=3,print***3.简单的星号(*)printing two
在主要的类函数里写一个分段码要求两个整数和output an n*m(n equals to row no, and m equals to column no)网格星号字符.
例子 n=4,m=5 *****
* *
* *
*****(1).你可以改一下你刚刚写的code去print一个星号字符的直角三角形代替一整个的n*n网格吗?
例子N=4 *
* *
* * *
* * * *
4.什么会是下面的分段码中的output?用netbeans查看之前记录下答案i. for(int i = 20; i > 2; i = i / 2)
System.out.print("i= "+i);ii. m = 10;
do
{
System.out.print("m= "+m);
m=m-3;
} while(m > 0);iii for(int speed = 13; speed > 0; speed -= 3)
System.out.print("Speed is "+speed);iv. for(int speed = 13; speed != 0; speed -= 3)
System.out.print("Speed is "+speed);5.改正loop中的错误找出下列分段码中的错误。
int n = 2, m = 2;
int sum = 0;for(int i = 0; i < n; i++;)
{
System.out.print("loop " + i);
for(int j == 0; j < m; i++);
{
sum += i * j;
}
System.out.println(“Sum= “ + sum);6.loop one
写一个分段码计算出第一个整数n的平方和(例子 1*1+2*2+...+n*n)7.loop two
写一个分段码print一个倒数计秒from 100 to 0 to the screen in increments of 5。8.在input中使用loop.
写一个分段码允许用户enter values和print out一些正数和负数.9.使用loop让input生效.
写一个while loop在数字1~100之间提示用户,连续提示到一个有效的入口完成。10.使用loop计算兀
一个计算兀的公式: 兀/2=2/1×2/3×4/3×4/5×6/5×6/7×8/7×8/9... (例子n=8)写一个程序可以在一个整数n中读出来,然后用第一个n的乘积公式output出兀的值提示:如果n=20,那么程序应该output出的值是3.067703806643497.(不是非常准确)11.用loop计算出斐波纳契数列.(Fibonacci sequence=斐波纳契数列是什么啊?=。=)
写一个分段码给出一个数字n,显示出斐波纳契数列的nth term,nth term是0,1,1,2,3,5,8,13......举个例子4th term应该是2. 0 if n=0;
Fn={ 1 if n=1;
Fn-1+Fn-2 if n>1. __________________________________________
| F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 |
___________________________________________
| 0 1 1 2 3 5 8 13 21 34|
____________________________________________
12.用loop计算出因数写一个while loop计算出n的因数
n!是定义为(0!=1):n!=n*(n-1)*(n-2)*(n-3)*..2*1的正整数
例子 4!=4*3*2*1=24
非常急,请各位XDJM帮帮小妹.谢谢
下面附上题目的英文原文1. Simple using while loop
The following code finds the sum of the first N numbers using a while loop. Put a print statement in the loop and follow the two variables (sum and i), during each iteration of the loop.
int sum = 0;
final int numTimes = 3;
int i = 1;
while ( i <= numTimes ){
sum += i;
i++;
}
System.out.print(“The sum of the first ”+numTimes);
System.out.print(“numbers is: ”+sum);
Alter the loop to sum the next N values from 5. i.e. 5+6+7+….
Alter the code to print out the numbers from N down to zero.
————————————————————————————————————————
2. Simple asterisk printing one
Write a code fragment in the main method to request an integer from the user and print that number of asterisk, (*), characters to the screen. E.g. N=3, print ***
——————————————————————————————————————————————
3. Simple asterisk printing two
Write a code fragment in the main method to request two integers and output an n*m (n equals to row no, and m equals to column no) grid of asterisk characters.
e.g. n = 4, m =5 *****
* *
* *
*****Can you change the code you have just written to print a right angle triangle of asterisk characters instead of a full n*n grid.
e.g. N = 4 *
* *
* * *
* * * *———————————————————————————————————————————————————————4.Simple loops
What will be the output of the following code fragments? Work it out and record your answer before checking in NetBeans.
i. for(int i = 20; i > 2; i = i / 2)
System.out.print("i= "+i);
ii. m = 10;
do
{
System.out.print("m= "+m);
m=m-3;
} while(m > 0);
iii for(int speed = 13; speed > 0; speed -= 3)
System.out.print("Speed is "+speed);
iv. for(int speed = 13; speed != 0; speed -= 3)
System.out.print("Speed is "+speed);
——————————————————————————————————————————————
5. Correct the errors in the loops
Spot the errors in the following code fragment. It is important that you can do this without the compiler, record and check your answers in the NetBeans afterwards.
int n = 2, m = 2;
int sum = 0;
for(int i = 0; i < n; i++;)
{
System.out.print("loop " + i);
for(int j == 0; j < m; i++);
{
sum += i * j;
}
System.out.println(“Sum= “ + sum);
————————————————————————————————————————————————————6. Simple for loop one
Write and test a code fragment to calculate the sum of the squares of the first n integers (i.e. 1*1 + 2*2 + ... +n*n) using a “for” loop
——————————————————————————————————————————
7. Simple for loop two
Write a code fragment that prints a countdown from 100 to 0 to the screen in increments of 5 using a “for” loop.
————————————————————————————————————————
8. Using loop in the input
Write a code fragment that allows the user to enter values and prints out the number of positive and the number of negative values entered.
————————————————————————————————————————————————————
9. Using loop to validate the input
Write a while loop that prompts the user for a number between 1 and 100 and continues to prompt the user until a valid entry is made.——————————————————————————————————————————————————10. Using loop to calculate 兀
A formula for calculating 兀 is 兀/2=2/1×2/3×4/3×4/5×6/5×6/7×8/7×8/9... (e.g. n = 8)Write a program that reads in an integer n and then outputs the value of 兀that is obtained by using the first n products of the formula
Check: If n is 20 the program should output the value 3.067703806643497. (Note: it is not very accurate!)
———————————————————————————————————————————————————————11. Using loop to calculate Fibonacci sequence
Write a code fragment that given a number n displays the nth term of the Fibonacci sequence, which is 0, 1, 1, 2, 3, 5, 8, 13….. For example the 4th term would be 2. 0 if n=0;
Fn={ 1 if n=1;
Fn-1+Fn-2 if n>1. __________________________________________
| F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 |
___________________________________________
| 0 1 1 2 3 5 8 13 21 34|
____________________________________________——————————————————————————————————12. Using loop to calculate factorial
Write a while loop to calculate n factorial.
n! is defined for a positive integer n as (0! =1): n! = n*(n-1)*(n-2)*(n-3)*…2*1
e.g. 4! = 4*3*2*1 =24
END
public static void main(String[] args) {
int i = getNum(5);
System.out.println(i);
}
public static int getNum(int n){
int sum = 0;
final int numTimes = 3;
int i = 1;
while(i <= numTimes){
sum += n++;
i++;
}
return sum;
}
}
回cx1014,我是真的不会..这个作业的成绩对我很重要的,如果会的话我不会来这里麻烦大家的。。
public static int getNum(int n){
int sum = 0;
final int numTimes = 3;
int i = 1;
while(i <= numTimes){
sum += n++;
i++;
}
return sum;
}
//2
public static void printStar(int i){
while(i > 0){
System.out.print("*");
i--;
}
}
//3
public static void printStar(int n, int m){
for (int i = 0; i< n; i++) {
for (int j = 0; j < m; j++) {
System.out.print("*");
}
System.out.println();
}
}
//3(1)
public static void printTriStar(int n){
for (int i = 0; i< n; i++) {
for (int j = -1; j < i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
//4 (i) i = 20; i = 10; ..... i = 4;
//4 (ii) m = 10; m = 7; m = 4; m = 1;
//4 (iii) Speed is 13 , 10, 7, 4 , 1;
//4 (iv) 死循环
//5 for(int j == 0; j < m; i++); //6
public static int getSum(int n){
int sum = 0;
for (int i = 1; i < n + 1; i++) {
sum += i * i;
}
return sum;
}
//7
public static void printInc(){
for (int i = 100; i >= 0; i -= 5) {
System.out.print(i + " ");
}
}
public static int getTheNthofFibonacci(int n){
if(n == 1) return 0;
if(n < 4) return 1;
int head = 1;
int end = 1;
int temp = 0;
while(n > 3){
temp = head + end;
head = end;
end = temp;
n--;
}
return temp;
}
//12
public static int getFactorial(int n){
int result = 1;
while(n > 0){
result *= n;
n--;
}
return result;
}
Write a code fragment that allows the user to enter values and prints out the number of positive and the number of negative values entered.
————————————————————————————————————————————————————
9. Using loop to validate the input
Write a while loop that prompts the user for a number between 1 and 100 and continues to prompt the user until a valid entry is made.
8.在input中使用loop
写一个允许用户输入数值并print out出它们的正数和负数.
9.用loop让input生效..
写一个while loop,提示介于1~100之间的用户,并持续提示直到一个有效的登记完成
公式里的项可分为下边子项
n / (n-1) * n / (n+1) 这些都是大于 1 的
最后 Pi 算出来肯定很大
//8
public static void printNum(){
System.out.println("Enter -1 to stop!");
Scanner sc = new Scanner(System.in);
while(true){
int i = sc.nextInt();
if (i == -1) {
break;
}
System.out.println(i);
System.out.println(-i);
}
}
//9
public static void numberGame(){
Scanner sc = new Scanner(System.in);
Random ran = new Random();
int temp = ran.nextInt(100) + 1;
while(true){
int i = sc.nextInt();
if(i == temp){
System.out.println("Congratulations, you are right");
break;
}else if(i < temp){
System.out.println("The right Number is bigger");
}else{
System.out.println("The right Number is smaller");
}
}
}
public static double getPi(int n){
double dPi = 1;
for (int i = 1; i <= n; i++) {
dPi *= 2*i / (2*i + Math.pow(-1, i));
}
return dPi * 2 ;
}
创建一个新文档(命名为Selection.java)来执行这些练习.直到现在我们讨论如果去写自己的类函数,我们所做的所有东西都会在一个中心,所有的练习都去创建一个新文档是让人讨厌的,所以,你会想去使用同一个文档,但是在保存答案之前,你可以构造一些像这样的程序:// Please input your information
public class Selection {
public static void main(String args[])
{
//Declarations
//Statements
//Sheet 2 Exercise 1: Complete
/*
//code your program here, comment out when complete
*/
//Sheet 2 Exercise 2: Currently working
//Etc }
}
1. Correct the errors in the following code
(Run the rectified code in the IDE and also record your change on the lab sheet)纠正一下代码的错误.( 在IDE里运行修正的code,把你的修改记录下来)
if x=y
x=x+1
System.print("incrementing x");
else
x=x-1;
System.print("Decrementing x);2. What does the following output and why?
(Run the rectified code in the IDE and record your results on the lab sheet)
Type it in and test your answer. Experiment with different values of a and b.
下面的output是什么,为什么?(在IDE里面运行修正的code,把结果记录下来)
测试结果,运用不同值得a和bint a = 1, b = 1;
if (a==1)
if (b==1)
System.out.println(“###”);
else
System.out.println(“***”);
System.out.println(“$$$”);
3. Write a nested if statement to display the following information
(Run the code in the IDE and save your program as Selection1.java)
写一个嵌入的if语句去显示下面的资料(在IDE里面运行code,然后作为Selection1.java保存下来.Speed Action (print to screen)
>80 Lose licence
70-80 3-6 points on licence
60-70 Caution
<60 Within speed limit4. Write a code fragment that reads in a double and outputs "Between 0 and 5.5" or "Not between 0 and 5.5", depending on the value read in.
(Run Run the code in the IDE and save your program as Selection2.java)写一段分代码,在double读出,并output出0~5.5之间,或者不在0~5.5之间.决定于你写出的值
(在IDE运行code然后作为Selection2.java保存下来)5. Write a code fragment to read in 3 integers and print out the largest.
(Run Run the code in the IDE and save your program as Selection3.java)写一个分段码,在3个整数里读出,并print out出最大的.(运行,作为Selection3.java保存)
前5道。。
if( x==y){
x=x+1 ;
System.out.print("incrementing x");
}else {
x=x-1;
System.out.print("Decrementing x");
}2.###3.
public class Selection1{
public static void main(String[] args) {
printMessage(75);
}
public static void printMessage(int i){
if (i > 80){
System.out.println("Lose licence");
}else if(i > 70){
System.out.println("3-6 points on licence");
}else if(i > 60){
System.out.println("Caution");
}else{
System.out.println("Within speed limit");
}
}
}
4.
public class Selection2{
public static void main(String[] args) {
opinionDoubleNum(4.6);
}
public static void opinionDoubleNum(double i){
if(i >= 0 && i <= 5.5){
System.out.println("Between 0 and 5.5");
}else{
System.out.println("Not between 0 and 5.5");
}
}
}
5.
public class Selection3{
public static void main(String[] args) {
System.out.println(getMax01(4,34,9));
System.out.println(getMax02(4,34,9));
}
public static int getMax01(int x, int y, int z){
return x > y ? x > z? x : z : y > z ? y : z;
}
public static int getMax02(int x, int y, int z){
if(x > y){
if(x > z)
return x;
return z;
}else{
if(y > z)
return y;
return z;
}
}
}
(Run Run the code in the IDE and save your program as Selection4.java)为了在1~50范围的数集创建一个直方图,我们需要找出来是什么bin a number会put into
(运行并作为Selection4.java保存)Bin 1 : 1-10
Bin 2 : 11-20
Bin 3 : 21-30
Bin 4 : 31-40
Bin 5 : 41-50这些bin会留出空间来给下面的资料.写一个分段码让用户输入一个1~50之间的数字,然后output出bin._________________________________________________________________7. Write a switch statement that prints a message
(Run Run the code in the IDE and save your program as Selection5.java)写一个print消息的swith语句
(在IDE运行并作为Selection5.java保存)
By entering a (from 0-100), a message will be displayed on the screen and indicates whether grade (a character) the is – 'A','B' or 'C', in which case print "You Have Passed", or 'D', 'E' in which case print "You have failed: You must resit the exam", or 'F', then print out "You have failed: You cannot resit the exam". Otherwise print out "Unknown Grade"利用输入一个0~100的符号,一个消息会显示在screen上,并会指出一个特性符号,A,B或者C,在一个案例里会print出You Have Passed,或者D,E,在一个案例中会print出You have failed:You must resit the exam,又或者f,会print out出you have failed:you cannot resit the exam.除了这些则会print out出unknown grade.Grade A: 70-100
Grade B: 60-70
Grade C: 50-60
Grade D: 40-50
Grade E: 30-40
Grade F: below 30
__________________________________________________________________8. Repeat exercise 7 using a nested if statement instead of a switch statement
(Run Run the code in the IDE and save your program as Selection6.java)重复练习的第7题,使用嵌入的if语句代替switch语句.
(运行并以Selection6.java保存)___________________________________________________9. Insert brackets to clarify the precedence of the following expressions
(Record your results on the lab sheet)插入括号来阐明下列优先的公式i. a>b == a<c || a>2 && b!=0
ii. a!=b && c>a || a<=d && c==0
今天要交了吧
正忙着 完事了吧?6.
public class Selection4{
public static void main(String[] args) {
printBin();
} public static void printBin() {
Scanner sc = new Scanner(System.in);
int temp = sc.nextInt() / 10;
switch (temp) {
case 0:
System.out.println("Bin 1");
break;
case 1:
System.out.println("Bin 2");
break;
case 2:
System.out.println("Bin 3");
break;
case 3:
System.out.println("Bin 4");
break;
case 4:
System.out.println("Bin 5");
break;
default:
System.out.println("The input number is not between 1 and 50");
break;
}
}
}
7.
public class Selection5{
public static void main(String[] args) {
printMess(68);
} public static void printMess(int i) {
int temp = i / 10;
switch (temp) {
case 10:
case 9:
case 8:
case 7:
System.out.println("Grade A :You Have Passed");
break;
case 6:
System.out.println("Grade B :You Have Passed");
break;
case 5:
System.out.println("Grade C :You Have Passed");
break;
case 4:
System.out.println("Grade D :You Have failed,You must resit the exam");
break;
case 3:
System.out.println("Grade E :You Have failed,You must resit the exam");
break;
case 2:
case 1:
case 0:
System.out.println("Grade F :You Have failed,You must resit the exam");
break;
default:
System.out.println("Unknown Grade");
break;
}
}
}
8.
public class Selection6{
public static void main(String[] args) {
printMess(68);
} public static void printMess(int i) {
if(i > 100) {
System.out.println("Unknown Grade");
}else if(i >= 70){
System.out.println("Grade A :You Have Passed");
}else if(i >= 60){
System.out.println("Grade B :You Have Passed");
}else if(i >= 50){
System.out.println("Grade C :You Have Passed");
}else if(i >= 40){
System.out.println("Grade D :You Have failed,You must resit the exam");
}else if(i >= 30){
System.out.println("Grade E :You Have failed,You must resit the exam");
}else{
System.out.println("Grade F :You Have failed,You cannot resit the exam");
}
}
}
9.
((a>b) == (a<c)) || (a>2) && (b!=0) // || 和 && 优先级一样 从左往右
(a!=b) && (c>a) || (a<=d) && (c==0)
case 0:
System.out.println("Grade F :You Have failed,You must resit the exam");
break;
default:
改下
case 0:
System.out.println("Grade F :You Have failed,You cannot resit the exam");
break;
default:
(Check your answers by compiling the code fragments in main; and record your results on the lab sheet)评估下列的逻辑表达式
(在main中用编译分段码检查你的答案,记录下来结果)-Insert brackets to clarify you understand the precedence rules.
插入括号去阐明你懂得的优先条件)int a=2,b=3,c=4,d=4;
char first='f', second='F';
i. a<b || c!=d
ii. a<b && c!=d
iii. (b==(a+1)) && c<=d && ((a+b)< (c+d)/2)
iv. first>'A' && first<'Z'
v. a>b == a<c || a>2 && b!=0
————————————————————————————11. De Morgan's laws for boolean algebra states that, given two boolean variables a and b
(Check your answers by compiling the code fragments in main; and save your program as Selection7.java)为了布尔代数德摩根定律, 给出了布尔变量a和b.
(在main中用编译分段码检查你的检查你的答案;并作为保存Selection7.java)i. NOT(a AND b) = NOT(a) OR NOT(b)
ii. NOT(a OR b) = NOT(a) AND NOT(b)Write Java expressions for these laws, assuming two boolean variables a and b have been declared. Verify that these laws are correct.为这些定律写一个java的公式,假如布尔变量a和b已经公布了,查证这些公式是正确的————————————————————————————————————————————
12. The cost of a ticket for a football match depends upon where you sit. The seats at the stadium are arranged in lettered blocks, where each block has a different ticket price.
(Save your program as Selection8.java)这些足球比赛门票的价钱决定了你的座位在哪里,露天的座位已经标记好了,每张不同价钱的票在哪个不同的区域?
(作为Selection8.java保存)Write a code fragment to calculate the price of a ticket based on the following information:写一个分段码去计算下列资料的一张票的基本价钱Seat座位 Price价钱
A, B: £24.00
C, E: £29.00
T, U: £20.00
H, N, O: £29.00
Q, J, R: £24.00
G, I, M, P: £25.00
Extend your code fragment to calculate the cost of 2 tickets. How about the cost for N tickets?补充你的分段码去计算两张票,N票是怎么样的?————————————————————————————————————————13. Write a program to calculate the cost of delivery for an order from Amazon.co.uk. The current rates for different items are shown below
(Save your program as AmazonDelivery.java)写一个程序去计算一个来自amazon.co.uk的商品送货的价钱,下面是最近的不同商品的送货情况.
(作为AmazonDelivery.java保存)Item Delivery(商品)cost per item(商品价钱) per delivery charge(送货价钱)
Book-------------------26p-------------------------£2.16
CD----------------------39p--------------------- £1.16
DVD---------------------49p------------------- £1.16
Game---------------------60p --------------------£1.59i.e1 Book – would cost a total of £2.42 (1 x £0.26 + £2.16)
例子1,book的总价会是£2.42 (1 x £0.26 + £2.16).i.e2 2 CDs + 1 Game – would cost a total of £2.97 (2 x £0.39 + 1 x £0.60 + £1.59) – the delivery charge is £1.59 as the delivery charge for a game is greater than that for a CD.
例子2,2张CD的价钱+1张游戏的总价是£2.97 (2 x £0.39 + 1 x £0.60 + £1.59),送货的钱是£1.59.The requirements of the program are:
程序的必要条件是:a) Ask the user to enter how many of each item they are buying;
b) Make sure that the input is valid, i.e. an integer greater than zero.
c) Calculate the cost of delivery.
d) Print out a neatly formatted break down of the delivery cost for example:a) 让用户输入每个产品他们都要买多少
b) 确定input是有效的,例子一个整数大过0
c)计算出送货的价钱
d)print out出一张简介的送货价钱的数据,例子:Item(商品) Number(数量) Cost(成本) Total(总金额)
Book--------2-----------0.26-------£0.52
CD----------4-----------0.39-------£1.56
DVD---------0-----------0.49-------£0.00
Game--------1-----------0.60-------£0.60
----------------------------------------------------------------------------
Delivery Charge(送货价钱)---------2.16-------£2.16
----------------------------------------------------------------------------
Total(总价)--------------------------£4.30
最后2道。。
10.
(a < b) || (c != d)
(a<b) && (c!=d)
(b==(a+1)) && (c<=d) && ((a+b)< (c+d)/2)
(first>'A') && (first<'Z')
((a>b) == (a<c)) || (a>2) && (b!=0)
11.
public class Selection7{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println(!(a&&b) == !a || !b);
System.out.println(!(a||b) == !a && !b);
}
}好忙啊
楼主这题重要么 做了这么多可以了吧
题目好长都不愿意看
你要是实在要交的话我就做做
要是无关紧要就不做了
12.
public class Selection8{
public static void main(String[] args) {
System.out.println(calculatePrice('A','C'));
System.out.println(calculatePrice('A','C','H'));
System.out.println(calculatePrice('A','C','O','Q'));
} //计算N张票价格,几张就填几个参数,
public static double calculatePrice(char... cs){
double result = 0.0;
for (int i = 0; i < cs.length; i++) {
result += getThePrice(cs[i]);
}
return result;
}
private static double getThePrice(char c){
switch (c){
case 'A':
case 'B':
return 24.00;
case 'C':
case 'E':
return 29.00;
case 'T':
case 'U':
return 20.00;
case 'H':
case 'N':
case 'O':
return 29.00;
case 'Q':
case 'J':
case 'R':
return 24.00;
case 'G':
case 'I':
case 'M':
case 'P':
return 25.00;
default:
return 0;
}
}
}最后那个题 我看不懂 商品价格哪来的?
例子2,2张CD的价钱+1张游戏的总价是£2.97 (2 x £0.39 + 1 x £0.60 + £1.59),送货的钱是£1.59.这两个怎么来的
(作为AmazonDelivery.java保存)Item Delivery(商品)cost per item(商品价钱) per delivery charge(送货价钱)
Book-------------------26p-------------------------£2.16
CD----------------------39p--------------------- £1.16
DVD---------------------49p------------------- £1.16
Game---------------------60p --------------------£1.59
book的价钱是26p,送货价£2.16
CD的钱加时39p,送货价£1.16
39p=£0.39
也就是100p=£1
public static void main(String[] args) {
Item book = new Item("Book",0.26,2.16);
Item cd = new Item("CD",0.39,1.16);
Item dvd = new Item("DVD",0.49,1.16);
Item game = new Item("Game",0.60,1.59);
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要 Book 数量");
int b = sc.nextInt();
while(b < 0){
System.out.println("请输入非负数");
b = sc.nextInt();
}
System.out.println("请输入需要 CD 数量");
int c = sc.nextInt();
while(c < 0){
System.out.println("请输入非负数");
c = sc.nextInt();
}
System.out.println("请输入需要 DVD 数量");
int d = sc.nextInt();
while(d < 0){
System.out.println("请输入非负数");
d = sc.nextInt();
}
System.out.println("请输入需要 Game 数量");
int g = sc.nextInt();
while(g < 0){
System.out.println("请输入非负数");
g = sc.nextInt();
}
double higEx = 0;
if(b > 0) higEx = Math.max(higEx, book.getExPrice());
if(c > 0) higEx = Math.max(higEx, cd.getExPrice());
if(d > 0) higEx = Math.max(higEx, dvd.getExPrice());
if(g > 0) higEx = Math.max(higEx, game.getExPrice());
System.out.println("Item\tNumber\tCost\tTotal");
System.out.println(book.getName()+"\t"+b+"\t"+book.getPrice()+"\t"+b*book.getPrice());
System.out.println(cd.getName()+"\t"+c+"\t"+cd.getPrice()+"\t"+c*cd.getPrice());
System.out.println(dvd.getName()+"\t"+d+"\t"+dvd.getPrice()+"\t"+d*dvd.getPrice());
System.out.println(game.getName()+"\t"+g+"\t"+game.getPrice()+"\t"+g*game.getPrice());
System.out.println("Delivery Charge\t" + higEx +"\t" + higEx);
System.out.println("Total\t\t\t" + (b*book.getPrice()+c*cd.getPrice()+d*dvd.getPrice()+g*game.getPrice()+higEx));
}
}
class Item{
private String name ;
private double price;
private double exPrice;
public double getExPrice() {
return exPrice;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
} public Item(String name, double price, double exPrice){
this.name = name;
this.price = price;
this.exPrice = exPrice;
}
}
请输入需要 Book 数量
2
请输入需要 CD 数量
4
请输入需要 DVD 数量
0
请输入需要 Game 数量
1
Item Number Cost Total
Book 2 0.26 0.52
CD 4 0.39 1.56
DVD 0 0.49 0.0
Game 1 0.6 0.6
Delivery Charge 2.16 2.16
Total 4.84
输出结果请输入需要 Book 数量
2
请输入需要 CD 数量
4
请输入需要 DVD 数量
0
请输入需要 Game 数量
1
Item Number Cost Total
Book 2 0.26 0.52
CD 4 0.39 1.56
DVD 0 0.49 0.0
Game 1 0.6 0.6
Delivery Charge 2.16 2.16
Total 4.84
//4 (ii) m = 10; m = 7; m = 4; m = 1;
//4 (iii) Speed is 13 , 10, 7, 4 , 1;
//4 (iv) 死循环
//计算N张票价格,几张就填几个参数,
public static double calculatePrice(char... cs){
double result = 0.0;
for (int i = 0; i < cs.length; i++) {
result += getThePrice(cs[i]);
这两个是要我把中文改成英文吗?
(Check your answers by compiling the code fragments in main; and save your program as Selection7.java)为了布尔代数德摩根定律, 给出了布尔变量a和b.
(在main中用编译分段码检查你的检查你的答案;并作为保存Selection7.java)i. NOT(a AND b) = NOT(a) OR NOT(b)
ii. NOT(a OR b) = NOT(a) AND NOT(b)Write Java expressions for these laws, assuming two boolean variables a and b have been declared. Verify that these laws are correct.为这些定律写一个java的公式,假如布尔变量a和b已经公布了,查证这些公式是正确的这道题题目落下了
11.
public class Selection7{
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println(!(a&&b) == !a || !b);
System.out.println(!(a||b) == !a && !b);
}
}
>>还有我问一下哦,这些题目你一共用了多久的时间的?
没事干扰的话 20 分钟左右吧