概述
2.9 Are the following statements correct? If so,show the output.
public class Demo {
public static void main(String[] args) {
System.out.println("25/4 is " + 25 / 4);
System.out.println("25/4.0 is " + 25 / 4.0);
System.out.println("3*2/4 is " + 3 * 2 / 4);
System.out.println("3.0*2/4 is " + 3.0 * 2 / 4);
}
}
2.18 Show the following output.
public class Demo {
public static void main(String[] args) {
float f = 12.5F;
int i = (int) f;
System.out.println("f is " + f);
System.out.println("i is " + i);
}
2.20 Which of the following are correct literals for characters?
public class Demo {
public static void main(String[] args) {
float f = 12.5F;
int i = (int) f;
System.out.println('1');
System.out.println('u3fFa');
System.out.println('b');
}
}
The 'u345dE' and t is wrong.
2.21 How do you display characters and "?
public class Demo {
public static void main(String[] args) {
System.out.println("\");
System.out.println(""");
}
}
2.22 Evaluate the following:
public class Demo {
public static void main(String[] args) {
int i = '1';
int j = '1' + '2';
int k = 'a';
char c = 90;
System.out.println("i=" + i + " j=" + j + " k=" + k + " c=" + c);
}
}
2.23 Can the following conversions involving casting be allowed? If so, find the converted result.
public class Demo {
public static void main(String[] args) {
char c = 'A';
float f = 1000.34f;
int i = (int) f;
i = (int) c;
System.out.println("i=" + i + " c=" + c + " f=" + f);
}
}
public class Demo {
public static void main(String[] args) {
double d = 1000.34;
int i = (int) d;
System.out.println("i=" + i + " d=" + d);
}
}
public class Demo {
public static void main(String[] args) {
int i = 97;
char c = (char) i;
System.out.println("i=" + i + " c=" + c);
}
}
2.24 Show the output of the following program:
public class Demo {
public static void main(String[] args) {
char x = 'a';
char y = 'c';
System.out.println(++x);
System.out.println(y++);
System.out.println(x - y);
}
}
2.25 Show the output of the following statements:
public class Demo {
public static void main(String[] args) {
System.out.println("1" + 1);
System.out.println('1' + 1);
System.out.println("1" + 1 + 1);
System.out.println("1" + (1 + 1));
System.out.println('1' + 1 + 1);
}
}
2.26 Evaluate the following expressions:
public class Demo {
public static void main(String[] args) {
System.out.println(1 + "welcome" + 1 + 1);
System.out.println(1 + "welcome" + (1 + 1));
System.out.println(1 + "welcome" + ('u0001' + 1));
System.out.println(1 + "welcome" + 'a' + 1);
}
}
2.6** Write a program that reads an integer between 0 and 100 and adds all the digits in the integer.For example,if an integer is 932,the sum of all its digits is 14.
import javax.swing.JOptionPane;
public class Demo {
public static void main(String[] args) {
int sum=0,number;
String input=JOptionPane.showInputDialog("Please input a number:");
number=Integer.parseInt(input);
while(number>0){
sum=sum+(number%10);
number/=10;
}
JOptionPane.showMessageDialog(null, "The digits is "+sum);
}
}
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int sum=0,number;
//String input=JOptionPane.showInputDialog("Please input a number:");
Scanner input=new Scanner(System.in);
number=input.nextInt();
while(number>0){
sum=sum+(number%10);
number/=10;
}
//JOptionPane.showMessageDialog(null, "The digits is "+sum);
System.out.println("The digits is "+sum);
}
}
2.24 Give an airplane's acceleration a and take-off speed v,you can compute the minimum runway length needed for an airplane to take off using the following formula:
length=v*v/2a
write a program that prompts the user to enter v in m/s and the acceleration a in m/s*s,and display the minimum runway length.
import javax.swing.JOptionPane;
public class Demo {
public static void main(String[] args) {
String v, a;
double v0, a0, length;
v = JOptionPane.showInputDialog("Please input the speed v:");
a = JOptionPane.showInputDialog("Please input the acceleration a:");
v0 = Double.parseDouble(v);
a0 = Double.parseDouble(a);
length = (v0 * v0) / (2 * a0);
java.text.DecimalFormat df = new java.text.DecimalFormat("#.###");
JOptionPane.showMessageDialog(null,
"The minimum length is " + df.format(length) + "m.", "Result",
JOptionPane.INFORMATION_MESSAGE);
}
}
import java.util.Scanner;
public class welcome {
public static void main(String[] args) {
double v0, a0, length;
Scanner input = new Scanner(System.in);
v0 = input.nextDouble();
a0 = input.nextDouble();
length = (v0 * v0) / (2 * a0);
java.text.DecimalFormat df = new java.text.DecimalFormat("#.###");
System.out.println("The minimum length is " + df.format(length) + "m.");
}
}
最后
以上就是复杂大米为你收集整理的第二周 - Elementary Programming的全部内容,希望文章能够帮你解决第二周 - Elementary Programming所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复