例3.2 输出单个字符
public class Demo3_2 {
public static void main(String[] args) {
char a, b, c;
a='B';
b='O';
c='Y';
System.out.print(a);
System.out.print(b);
System.out.print(c);
System.out.println();
System.out.print((char) 66);
System.out.print((char) 79);
System.out.print((char) 89);
System.out.print((char) 10);
}
}
例3.3 输入单个字符
import java.util.Scanner;
public class Demo3_3 {
public static void main(String[] args) {
char c;
Scanner sc = new Scanner(System.in);
c = sc.next().charAt(0);
System.out.println((char)(c+32));
}
}
例3.4 输入输出
public class Demo3_4 {
public static void main(String[] args) {
int a;
float b;
char c;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextFloat();
c = sc.next().charAt(0);
System.out.println("a="+a+",b="+b+",c="+c);
}
}
例3.5 求一元二次方程式ax2 + bx+c=0的根。a, b, c的值在运行时由键盘输入,它们的值满足b2-4ac≥0。
import java.util.Scanner;
public class Demo3_5 {
public static void main(String[] args) {
float a, b, c, x1, x2;
Scanner sc = new Scanner(System.in);
a = sc.nextFloat();
b = sc.nextFloat();
c = sc.nextFloat();
x1 = (float) ((-b + Math.sqrt(b*b-4*a*c))/(2*a));
x2 = (float) ((-b - Math.sqrt(b*b-4*a*c))/(2*a));
System.out.println("x1 = "+x1);
System.out.println("x2 = "+x2);
}
}
例3.6 求三角形面积
import java.util.Scanner;
public class Demo3_6 {
public static void main(String[] args) {
double a, b, c;
System.out.println("Please enter a, b, c:");
Scanner sc = new Scanner(System.in);
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
if(a+b>c && b+c>a && c+a>b) {
double s, area;
s = (a+b+c)/2;
area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("area = "+area);
} else
System.out.println("It is not a trilateral!");
}
}
例3.7 输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母;如果不是,不转换。然后输出最后得到的字符
import java.util.Scanner;
public class Demo3_7 {
public static void main(String[] args) {
char ch;
Scanner sc = new Scanner(System.in);
ch = sc.next().charAt(0);
ch = (ch >= 'A'&&ch<='Z')?(char)(ch+32):ch;
System.out.println(ch);
}
}
例3.8 编写程序,判断某一年是否为闰年
import java.util.Scanner;
public class Demo3_8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year;
boolean leap;
System.out.println("Please enter year:");
year = sc.nextInt();
if(year%4 ==0){
if(year%100 == 0){
if(year%400 == 0){
leap = true;
} else
leap =false;
} else
leap =false;
} else
leap = false;
if(leap)
System.out.print(year+" is");
else
System.out.print(year+" is not");
System.out.println(" a leap year.");
}
}
import java.util.Scanner;
public class Demo3_9 {
public static void main(String[] args) {
int c,s;
float p, w, d = 0, f;
System.out.print("Please enter p, w, s:");
Scanner sc = new Scanner(System.in);
p = sc.nextInt();
w = sc.nextInt();
s = sc.nextInt();
if(s >=3000)
c = 12;
else
c = s/250;
switch (c) {
case 0:d=0;break;
case 1:d=2;break;
case 2:
case 3:d=5;break;
case 4:
case 5:
case 6:
case 7:d=8;break;
case 8:
case 9:
case 10:
case 11:d=10;break;
}
f = (float) (p*w*s*(1-d/100.0));
System.out.println("freight="+f);
}
}
例3.10 求1+2+3+...+100的值
public class Demo3_10 {
public static void main(String[] args) {
int i =1;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println("sum="+sum);
}
}