概述
编写HelloWorld,打印出“HelloWorld”,并进行编译调试//HelloWorld.java
Java代码:
package first;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("HelloWorld");
}
}
2:(Rectangle.java)输入长方形的长,宽,得出总面积,并打印出总面积。并利用printf进行格式化输出。//Rectangle.java
Java代码:
package first;
import java.util.Scanner;
public class Rectangle {
public static void main(String atgs[]) {
Demo rect = new Demo();
Scanner reader = new Scanner(System.in);
try {
System.out.println("输入矩形的长,并回车确认");
rect.height = reader.nextDouble();
System.out.println("输入矩形的宽,并回车确认");
rect.width = reader.nextDouble();
double area = rect.getArea();
System.out.println("矩形的面积:" + area);
} finally {
reader.close();
}
}
}
class Demo {
double height;
double width;
double getArea() {
return height * width;
}
}
3:(Score.java)反复输入一个整数,如果不是-1,则判断
如果小于60分,则为不及格,小于100分,大于60分为及格,其它输入为输入错误。
若为-1,则退出。 //Score.java
Java代码:
package first;
import java.util.Scanner;
public class Score {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
try {
while (true) {
System.out.println("输入一个整数");
int score = reader.nextInt();
if (score == -1) {
System.out.println("程序已退出");
break;
} else if (score >= 0 && score < 60) {
System.out.println("成绩不及格");
} else if (score >= 60 && score <= 100) {
System.out.println("成绩及格");
} else if (score > 100 || score < -1) {
System.out.println("输入错误");
}
}
} finally {
reader.close();
}
}
}
4:用for语句完成从1加到100. //He.java
Java代码:
package first;
public class He {
public static void main(String atgs[]) {
int i = 1;
int sum = 0;
for (i = 1; i <= 100; i++) {
sum = sum + i;
}
System.out.println("1加到100的和:" + sum);
}
}
5:完成p30 实验2 //GreekAlphabet.java
Java代码:
package first;
public class GreekAlphabet {
public static void main(String args[]) {
int startPosition = 0, endPosition = 0;
char cStart = 'α', cEnd = 'ω';
startPosition = (int) (cStart - 48);
endPosition = (int) (cEnd - 48);
System.out.println("希腊字母'α'在unicode表中的顺序位置:" + startPosition);
System.out.println("希腊字母表: ");
for (int i = startPosition; i <= endPosition; i++) {
char c = '