华南理工大学大一JAVA复习题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
2.1从输入对话框读入double型的华氏度,将其转换为摄氏度,并在消息对话框中显示结果。Celsius = (5/9)*(Fahrenheit-32)
import javax.swing.JOptionPane;
public class Exercise2_1 {
public static void main(String[] args) {
String fahrenheitString = JOptionPane.showInputDialog(null, "Enter a temperature in fahrenheit:",
"Exercise2_1 Input", JOptionPane.QUESTION_MESSAGE);
double fahrenheit = Double.parseDouble(fahrenheitString);
double celsius = (5.0 / 9.0) * (fahrenheit - 32);
System.out.println("The temperature is " +
celsius + " in Celsius");
System.exit(0);
}
}
2.7编写程序将大写字母转换为小写字母,该字符在源代码ASCII中指定为直接量。
public class Exercise2_7 {
public static void main(String[] args) {
char uppercase = 'F';
int offset = (int)'a' - (int)'A';
char lowercase = (char)((int)uppercase + offset);
System.out.print("The lowercase letter is " + lowercase);
}
}
3.1 (三角形有效性验证)读入三角形的三条边并确定输入是否有效。如果任意两条边和大于第三边则输入有效。
import javax.swing.JOptionPane;
public class Exercise3_1 {
public static void main(String[] args) {
String numberString = JOptionPane.showInputDialog(null,
"Enter the first edge length (double)",
"Exercise3_1 Input", JOptionPane.QUESTION_MESSAGE);
double edge1 = Double.parseDouble(numberString);
numberString = JOptionPane.showInputDialog(null,
"Enter the second edge length (double)",
"Exercise3_1 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string to double
double edge2 = Double.parseDouble(numberString);
numberString = JOptionPane.showInputDialog(null,
"Enter the third edge length (double)",
"Exercise3_1 Input", JOptionPane.QUESTION_MESSAGE);
double edge3 = Double.parseDouble(numberString);
System.out.println("Can edges " + edge1 + ", " + edge2 + ", and "
+ edge3 + " form a triangle? " + (
(edge1 + edge2 > edge3) && (edge1 + edge3 > edge2) &&
(edge2 + edge3 > edge1)));
System.exit(0);
}
}
4.1 (千克转换成磅)编一个显示下列表格的程序(1千克为2.2磅)
(1kilogram = 2.2 pounds)
Kilograms Pounds
1 2.2
3 6.6
……
197 433.4
199 437.8
public class Exercise3_8 {
public static void main(String[] args) {
System.out.println("kilograms\t\tpounds");
System.out.println("-------------------------");
int kilograms = 1;
for (int i = 1; i <= 100; kilograms += 2, i++) {
System.out.println(kilograms + "\t\t" + kilograms * 2.2); }
}
}
4.18 用嵌套的循环语句,分别编写程序打印下列图案。
Pattern 1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5