想要的运行截图是这个样子!请把整个代码给我,谢谢啦!
当电脑状态为1时,输出为:
电脑开机,运行中
毕老,上课中
当电脑状态为2时,输出为:
电脑蓝屏了
电脑重启
毕老上课中
当电脑状态为3时,输出为:
做练习
NoclassException:电脑冒烟了。。。,无法继续上课,自习或者放假
import java.util.Scanner;
public class ComputeTest {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int type=in.nextInt();
switch (type){
case 1:
System.out.println("电脑开机,运行中");
System.out.println("毕老,上课中");
break;
case 2:
System.out.println("电脑蓝屏了");
System.out.println("电脑重启");
System.out.println("毕老上课中");
break;
case 3:
System.out.println("做练习");
System.out.println("NoclassException:电脑冒烟了。。。,无法继续上课,自习或者放假");
break;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入电脑状态:");
int flag = scanner.nextInt();
if (1 == flag) {//当电脑状态为1时,输出为:
System.out.println("电脑开机,运行中");
System.out.println("毕老,上课中");
} else if (2 == flag) {//当电脑状态为2时,输出为:
System.out.println("电脑蓝屏了");
System.out.println("电脑重启");
System.out.println("毕老上课中");
} else if (3 == flag) {//当电脑状态为3时,输出为:
System.out.println("做练习");
System.out.println("NoclassException:电脑冒烟了。。。,无法继续上课,自习或者放假");
}
}
注意:当校验条件不超过 “5” 种可能时;建议使用if进行校验 (超过则使用 switch::前提是相等)
就是一个简单的switch case 就能解决的,剩下的不就是输出么
楼上正解,switch case ; if else ; 策略 都行
简单的switch case 、 if else即可实现
电脑状态是电脑自身状态还是你输入的状态?
代码如下:
public class Answer7702106 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请输入电脑状态(1,2,3)");
int status = scanner.nextInt();
switch (status) {
case 1:
System.out.println("电脑开机,运行中\n毕老,上课中");
break;
case 2:
System.out.println("电脑蓝屏了\n电脑重启\n毕老上课中");
break;
case 3:
System.out.println("做练习\nNoclassException:电脑冒烟了。。。,无法继续上课,自习或者放假");
break;
}
}
}
}
运行结果如下:
请输入电脑状态(1,2,3)
1
电脑开机,运行中
毕老,上课中
请输入电脑状态(1,2,3)
2
电脑蓝屏了
电脑重启
毕老上课中
请输入电脑状态(1,2,3)
3
做练习
NoclassException:电脑冒烟了。。。,无法继续上课,自习或者放假
请输入电脑状态(1,2,3)
如有帮助,请采纳,十分感谢!
就是简单的条件判断程序呀,可以使用if-else也可以使用switch-case条件分支
有帮助请采纳,谢谢!
class lanpingexception extends exception //自定义蓝屏异常
{
lanpingexception(string message)
{
super(message);
}
}
class maoyanexception extends exception//自定义冒烟异常
{
maoyanexception(string message)
{
super(message);
}
}
class noclassexception extends exception//自定义不能上课异常
{
noclassexception(string message)
{
super(message);
}
}
class computer
{
private int state = 2;//硬性规定电脑状态
//问题是在运行时发生的,所以得定义在运行时
public void run()throws lanpingexception,maoyanexception//因为有可能出现错误,所以必须先声明
{
if (state==2)
{
throw new lanpingexception("电脑蓝屏了。。。");//因为电脑不能自己处理,所以抛给了老师
}
if (state==3)
{
throw new maoyanexception("电脑冒烟了。。。");
}
system.out.println("电脑开机,运行中。。。。");
}
public void restart()
{
system.out.println("电脑重启。。。。");
}
}
class teacher
{
private string name;
private computer comp;
teacher(string name)
{
this.name = name;
comp = new computer();//初始化时老师就有电脑了
}
public void test()
{
system.out.println("做练习");
}
public void teach() throws noclassexception//标识应该是抛出去的异常,能够进行处理的
{
try
{
comp.run();//讲课,要先开启电脑
}
catch (lanpingexception e)//抛出了两个异常,必须用两个catch
{
system.out.println(e.getmessage());
comp.restart();
}
//这个问题抛给老师的话依旧无法解决,应该抛出对应的问题,这里老师抛出的问题应该是不能上课了
catch (maoyanexception e)
{
test();//不能上课,可以先布置练习,这个必须在throw 之前,因为throw之后的语句不会执行
throw new noclassexception(e.getmessage()+",无法继续上课");
}
system.out.println(name+",上课中。。。。");
}
}
class exceptiontest
{
public static void main(string[] args)
{
teacher t = new teacher("毕老师");//指定一个老师
try
{
t.teach();//老师运行这个方法
}
catch (noclassexception e)//抓住对应的抛出的问题
{
system.out.println(e.tostring());//打印出问题原因
system.out.println("自习或者放假");//处理办法
}
}
}