1. 在IDE中輸入以下代碼,並觀察運行結果。
import java.util.Scanner;
public class ComputeAverage
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter three numbers:");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double average = (number1 + number2 + number3)/3;
System.out.println("The average of " + number1 + " " + number2 + " "+ number3 + " is " + average);
}
}
2. 根據題目要求,編寫程序完成相應要求:
使用Java語言,使用System.in輸入以下各個變量的值,並按照公式計算得出結果。
公式爲:
3. 編寫程序接收一個ASCII碼(從0至128),然後顯示輸入ASCII碼所代表的字符。
注意:
(1)能夠對1個或多個輸入的ASCII碼進行處理;
(2)對於輸入的非ASCII碼進行判定和排除。
4. 編寫一個程序,顯示下面的圖案:
|
|
|
J |
|
|
|
|
A |
|
|
|
|
V |
|
|
|
|
|
V |
|
|
|
|
A |
|
|
|
|
|
|
J |
|
|
|
A |
|
A |
|
|
|
|
V |
|
|
|
V |
|
|
|
|
A |
|
A |
|
|
J |
|
|
J |
|
|
A |
A |
A |
A |
A |
|
|
|
|
V |
|
V |
|
|
|
|
A |
A |
A |
A |
A |
|
|
J |
J |
|
|
A |
|
|
|
|
|
A |
|
|
|
|
V |
|
|
|
|
A |
|
|
|
|
|
A |
1、
package org.cust.test1;
import java.util.Scanner;
public class ComputeAverage
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter three numbers:");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double average = (number1 + number2 + number3)/3;
System.out.println("The average of " + number1 + " " + number2 + " "+ number3 + " is " + average);
input.close();
}
//求三個輸入的數的平均數
}
2、
package org.cust.test1;
import java.util.Scanner;
public class Test_1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double a, b, c, d, r, result;
while (true) {
System.out.println("請依次輸入a,b,c,d,r的數值如下:");
a = in.nextDouble();
b = in.nextDouble();
c = in.nextDouble();
d = in.nextDouble();
r = in.nextDouble();
if (r == -34) {
System.out.println("r值輸入錯誤");
} else if (-a == b * d) {
System.out.println("a,b,d值輸入錯誤");
} else {
result = 4 / (3 * r + 34) - 9 * (a + b * c) + (3 + d * (2 + a)) / (a + b * d);
System.out.println("計算結果爲:" + result);
}
}
}
}
3、
package org.cust.test1;
import java.util.Scanner;
public class Test_2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String asc;
int j;
System.out.println("請輸入:");
asc = in.nextLine();
String[] chars = asc.split(" ");
char[] output = new char[chars.length];
for (int i = 0; i < chars.length; i++) {
j = Integer.parseInt(chars[i]);
if (j > 128 || j < 0) {
System.out.println(j + "在所輸入字段中不屬於ASCII碼");
} else {
output[i] = (char) j;
}
}
System.out.println("轉換結果如下:");
for (int y = 0; y < output.length; y++) {
System.out.print(output[y] + " ");
}
in.close();
}
}
4、
package org.cust.test1;
public class Test_3 {
public static void main(String[] args) {
char[][] j = new char[4][4];
char[][] a1 = new char[4][7];
char[][] v = new char[4][7];
char[][] a2 = new char[4][7];
for (int i = 0; i < j.length; i++) {
for (int y = 0; y < j[i].length; y++) {
j[i][y] = ' ';
}
}
for (int i = 0; i < a1.length; i++) {
for (int y = 0; y < a1[0].length; y++) {
a1[i][y] = ' ';
}
}
for (int i = 0; i < v.length; i++) {
for (int y = 0; y < v[0].length; y++) {
v[i][y] = ' ';
}
}
for (int i = 0; i < a2.length; i++) {
for (int y = 0; y < a2[0].length; y++) {
a2[i][y] = ' ';
}
}
j[0][3] = 'J';
j[1][3] = 'J';
j[2][0] = 'J';
j[2][3] = 'J';
j[3][1] = 'J';
j[3][2] = 'J';
for (int i = 0; i < a1.length; i++) {
for (int y = 0; y < a1[0].length; y++) {
if (y == 3 - i || y == 3 + i) {
a1[i][y] = 'A';
a2[i][y] = 'A';
}
}
}
for (int i = 0; i < v.length; i++) {
for (int y = 0; y < v[0].length; y++) {
if (y == i || y == 6 - i) {
v[i][y] = 'V';
v[i][y] = 'V';
}
}
}
for(int i = 0; i<4; i++){
for(int y = 0; y<29; y++){
if(y==4||y==13||y==21)
System.out.print(' ');
if(y<4){
System.out.print(j[i][y]);
}
else if(y>4&&y<12){
System.out.print(a1[i][y-5]);
}
else if(y>13&&y<21){
System.out.print(v[i][y-14]);
}
else if (y>21){
System.out.print(a2[i][y-22]);
}
}
System.out.println();
}
}
}