Welcome to JAVA!(第二课课后练习)

2.9 Show the output.

<pre class="java" name="code">public class Main {
	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 follow output.

public class Main {
	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 Main {
	public static void main(String[] args)
	{
		System.out.println('1');
		System.out.println('\u345dE');
		System.out.println('\u3fFa');
		System.out.println('\b');
		System.out.println(\t);
	}

}


经过测试,'\u345dE'和\t不是正确的字符。

 

2.21 How do you display characters \ and "?

public class Main {
	public static void main(String[] args)
	{
		System.out.println(""");
		System.out.println("\"");
		System.out.println("\");
		System.out.println("\\");
	}

}


经过测试,想要显示\和"需要在\和"前面加上\并添加" "。直接输出时是错误的。

 

2.22 Evaluate the following

public class Main {
	public static void main(String[] args)
	{
		int i = '1';
		int j = '1' + 2;
		int k = 'a';
		char c = 90;
		System.out.println(i);
		System.out.println(j);
		System.out.println(k);
		System.out.println(c);
	}

}


结果如下:

49
51
97
Z

 


2.23 Can the following conversions involving casting be allowed? if so,find the converted result.

错误代码如下:

public class Main {
	public static void main(String[] args)
	{
		char c ='A';
		i = (int)c;
		System.out.print(i);
		float f = 1000.34f;
		int i = (int)f;
		System.out.print(i);
		double d = 1000.34;
		int i = (int)d;
		System.out.print(i);
		int i = 97;
		char c = (char)i;
		System.out.print(i);
	}

}


修改后:

public class Main {
	public static void main(String[] args)
	{
		char c ='A';
		int i = (int)c;
		System.out.println(i);
		
		float f = 1000.34f;
		i = (int)f;
		System.out.println(i);
		
		double d = 1000.34;
		i = (int)d;
		System.out.println(i);
		
	    i = 97;
		c = (char)i;
		System.out.println(c);
	}

}


运行结果:

65
1000
1000
a

 


2.24 Show the output of the following program

public class Main {
	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);
	}

}


运行结果:

b
c
-2


 

2.25 Show the output of the following statements(write a program to verify your result);

public class Main {
	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);
	}

}


运行结果:

11
50
111
12
51


 

2.26 Evaluate the following expressions(write a program to verify your result)

public class Main {
	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);

	}

}


运行结果:

1 Welcome 11
1 Welcome 2
1 Welcome 2
1 Welcome a1

 


2.6 Write a program that rads an integer between 0 and 1000 and adds all the digits in the integer.For example,if an integer is 932.the sum of all its digits is 14.

(1)

import java.util.Scanner;
public class Main {
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a number between 0 and 1000:");
		int a=input.nextInt();
		int sum=0;
		while (a>0)
		{
			sum+=a%10;
			a/=10;
		}
		System.out.println("The sum of the digit is " + sum);

	}

}

(2)

import javax.swing.JOptionPane;
public class Main {
	public static void main(String[] args)
	{
		String input=JOptionPane.showInputDialog("Enter a number between 0 and 1000:");
		int a=Integer.parseInt(input);
		int sum=0;
		while (a>0)
		{
			sum+=a%10;
			a/=10;
		}
		JOptionPane.showMessageDialog(null, "The sum of the digit is " + sum);

	}

}


 

运行结果:

Enter a number between 0 and 1000:999
The sum of the digit is 27


2.24 Given 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 follow formula:length=v^2/2a;

Write a program that promts the user to enter v in meters/second(m/s) and the acceleration a in meters/second squared(m/s^2),and displays the minimum runway length.

 (1)

import java.text.DecimalFormat;
import java.util.Scanner;
public class MAIN
{
	public static void main(String[] args) 
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter v and a:");
		double v=input.nextDouble(),a=input.nextDouble();
		double length;
		length=(v*v)/(2*a);
		DecimalFormat df = new DecimalFormat("0.000");
		System.out.println("The minimum runway length for this airplane is " +df.format(length));
	}
}

(2)

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Main {
	public static void main(String[] args) {
		String input1 = JOptionPane.showInputDialog("Enter v:");
		String input2 = JOptionPane.showInputDialog("Enter a:");
		double v = Double.parseDouble(input1), a = Double.parseDouble(input2);
		System.out.println("v= "+v +" a="+a);
		double length;
		length = (v * v) / (2 * a);
		DecimalFormat df = new DecimalFormat("0.000");
		JOptionPane.showMessageDialog(null,"The minimum runway length for this airplane is "
						+ df.format(length));
	}
}

 运行结果:

Enter v and a:60 3.5
The minimum runway length for this airplane is 514.286




 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值