目录
09: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,如果大于60就提示输入错误。
如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;
11:随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
12:使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
13:使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
09: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,如果大于60就提示输入错误。
package WorkDemo2;
import java.util.Scanner;
public class DemoText2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入1-12之间的小时数:");
int hour = sc.nextInt();
System.out.println("请输入1-24之间的分钟数:");
int minute = sc.nextInt();
System.out.println("请输入1-24之间的秒数:");
int second = sc.nextInt();
if (hour >12 || hour<1 || minute >60 || minute<1 || second>60 || second<1){
System.out.println("输入错误,请检查一下重新输入");
}
if (hour<10){
System.out.print("0"+hour+":");
}else
System.out.println(hour+":");
if (minute <10){
System.out.print("0"+minute+":");
}else
System.out.print(minute+":");
if (second<10){
System.out.print("0"+second);
}else
System.out.print(second);
System.out.println();
}
}
10: 有3个整数,给出提示信息:
能否创建三角形;
如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;
最后输出三角形面积;
这个代码不是很好,其中的三角形运算公式应该是:海伦公式:
s=Math.sqrt(p*(p-a)*(p-b)*(p-c))
package WorkDemo2;
import java.util.Scanner;
public class DemoText3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的三条边:");
System.out.println("请输入第一条边的值:");
int one = sc.nextInt();
System.out.println("请输入第二条边的值:");
int two = sc.nextInt();
System.out.println("请你输入第三条边的值:");
int three = sc.nextInt();
if (one+two<three){
System.out.println("你输入的三条边构不成三角形,请重新输入");
}else if(one==two || two==three || three==one){
System.out.print("这是等腰三角形:");
if(one == two){
double number=Math.sqrt(Math.pow(two, 2)-Math.pow((three/2.0),2));
System.out.println("三角形的面积是:"+((three*number)/2));
}else if(one == three){
double number=Math.sqrt(Math.pow(three, 2)-Math.pow((two/2.0),2));
System.out.println("三角形的面积是:"+((two*number)/2));
}else{
double number=Math.sqrt(Math.pow(three, 2)-Math.pow((one/2.0),2));
System.out.println("三角形的面积是:"+((one*number)/2));
}
}else if(Math.pow(one, 2)+Math.pow(two, 2)==Math.pow(three, 2) || Math.pow(one, 2)+Math.pow(three, 2)==Math.pow(two, 2) || Math.pow(three, 2)+Math.pow(two, 2)==Math.pow(one, 2)){
System.out.println("这是等腰直角三角形");
if(Math.pow(one, 2)+Math.pow(two, 2)==Math.pow(three, 2)){
System.out.println("三角形的面积是:"+(one*two)/2);
}else if(Math.pow(one, 2)+Math.pow(three, 2)==Math.pow(two, 2)){
System.out.println("三角形的面积是:"+(one*three)/2);
}else{
System.out.println("三角形的面积是:"+(two*three)/2);
}
}else{
System.out.println("这是普通三角形");
double number1 = (one+two+three)/2;
System.out.println("三角形的面积是:"+Math.sqrt(number1*(number1-one)*(number1-two)*(number1-three)));
}
}
}
11:随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
package WorkDemo4;
import java.util.Scanner;
public class IfDemo1 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符:");
String ziFu = sc.next();
// charAt是从0开始,而不是以,赋值第一个字符,就是0,第二个就是1
char aa = ziFu.charAt(0);
// 小a的ASCII值是大于A的,所以说如果是小a则减去31
if((aa >= 'a'&& aa<='z')){
System.out.println("大写字母是:"+(char)((int)aa-32));
}else if(aa>='A' && aa<='Z'){
System.out.println("小写字母是:"+(char)((int)aa+32));
}else{
System.out.println("你输入的字符没有大写或小写字符!");
}
}
}
12:使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
package WorkDemo2;
import java.util.Scanner;
public class DemoText4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的成绩");
double number = sc.nextDouble();
if(number>=90){
System.out.println("A");
}else if(number>=60){
System.out.println("B");
}else{
System.out.println("C");
}
}
}
13:使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
如果岳灵珊的考试成绩==1OO分,父亲给她买辆车
如果岳灵珊的考试成绩>=90分,母亲给她买台笔记本电脑
如果岳灵珊的考试成绩>=60分,母亲给她买部手机
如果岳灵珊的考试成绩<60分,没有礼物
package WorkDemo2;
import java.util.Scanner
public class DemoText5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入Java成绩");
int number = sc.nextInt();
if(number==100){
System.out.println("这是给你的车");
}else if(number>=90){
System.out.println("这是你的电脑");
}else if(number>=60){
System.out.println("这是你的手机");
}else{
System.out.println("没有礼物");
}
}
}
14:使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。
package WorkDemo2;
import java.util.Scanner;
public class DemoText6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请你输入你的用户名:");
String charUser = sc.next();
System.out.println("请你输入你的密码");
String charId = sc.next();
if(charUser.equals("宝宝") && charId.equals("123")){
System.out.println("欢迎你,宝宝");
}else
System.out.println("对不起,你不是宝宝");
}
}
15:求ax2+bx+c=0方程的根。
a,b,c分别为函数的参数,
如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
已知:
x1=(-b+sqrt(b2-4ac))/2a
x2=(-b-sqrt(b2-4ac))/2a
package WorkDemo2;
import java.util.Scanner;
public class DemoText7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入三个数a,b,c:");
System.out.println("输入a:");
int a=sc.nextInt();
System.out.println("输入b:");
int b= sc.nextInt();
System.out.println("输入c:");
int c= sc.nextInt();
if((Math.pow(b,2)-4*a*c)>0){
double X1=(-b+Math.sqrt(Math.pow(b,2)-4*a*c)/2);
double X2=(-b-Math.sqrt(Math.pow(b,2)-4*a*c)/2);
System.out.println("这个式子的第一个根是:"+X1);
System.out.println("这个式子的第二个根是:"+X2);
}else if((Math.pow(b,2)-4*a*c)==0){
double X1=(-b+Math.sqrt(Math.pow(b,2)-4*a*c)/2);
System.out.println("他有两个一样的根,根为:"+X1);
}else
System.out.println("这个式子没有根");
}
}
下面还有两题,代码有一点多,但好理解,若有错误,请指正
都是我自己写的,呜呜呜,给个鼓励呗