如何检查以下代码中的条件? [英] How do I check condition in the below code?

查看:95
本文介绍了如何检查以下代码中的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;

public class Parent {

	public static void main(String[] args) 
	{
		Scanner scan=new Scanner(System.in);
		
		System.out.println("enter the 1st no");
		int p=scan.nextInt();
		
		System.out.println("enter the 2nd no");
		int q=scan.nextInt();
		
		System.out.println("enter the 3rd no");
		int r=scan.nextInt();
		
		System.out.println("enter the 4th no");
		int s=scan.nextInt();
		
		Sub obj=new Sub();
		Sub obj1=new Sub(p,q);
		Sub obj2=new Sub(p,q,r);
		Sub obj3=new Sub(p,q,r,s);
		
		obj.get();
		obj1.get();
		obj2.get();		
		obj3.get();
		
		
		
	 }

}




<pre>import java.util.Scanner;

public class Sub 
{
	int w=1,x=1,y=1,z=1;
	public Sub()
	{
		x=5;
		y=4;
	}
	public Sub(int a,int b)
	{
		x=a;
		y=b;
	}
	public Sub(int a,int b,int c)
	{
		x=a;
		y=b;
		w=c;
	}
	public Sub(int a,int b,int c,int d)
	{
		x=a;
		y=b;
		w=c;
		z=d;
		
	}
	public void get()
	{
		Scanner scan=new Scanner(System.in);
		System.out.println("choose the no. of operation you want to do");
		System.out.println("  1 --> +  \n  2--> - \n  3--> * \n  4--> /");
		int k=scan.nextInt();
		
		switch(k)
		
		{
		case 1: 
			
			double c22 = w+x+y+z;
			
			int i=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
         //ERROR : a,b,c are not defined.
			(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
			(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	
			
			 	System.out.println(i);
			
			System.out.println("addition of two no. is :  " +c22);
			break;
		
		case 2: 
			
			double c1 = w-x-y-z;

			int j=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
	//ERROR : a,b,c are not defined.
			(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
				(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	
				
				System.out.println(j);
				
			System.out.println("subtration of two no. is :  " +c1);
			break;
	
		case 3: 
			
			double c11 = w*x*y*z;
			
			int h=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
	//ERROR : a,b,c are not defined.
		(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
				(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	

				System.out.println(h);
				
			System.out.println("multiplication of two no. is :  " +c11);
			break;
		
		case 4: 
			
			double f = w/x/y/z;
			
			int g=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
           //ERROR : a,b,c are not defined.
				(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
				(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	
				
				System.out.println(g);
				
			System.out.println("division of two no. is :  " +f);
			break;
			
        default :
        	System.out.println("you choose wrong option.\n     "
        			+ "     please i request you to choose one of the option listed above  !!");
	//IMPORTANT	  
        	get();
        	break;
		}
	}
}







什么我试过:



在上面的代码我想检查输入是否与我使用切换方法和三元条件的构造函数相匹配但在三元条件下,当我检查w == a等时,它不会检查它..





如何检查这样的条件?




What I have tried:

in the above code i want to check whether the input are matched with which constructor for which i have use switch method and ternary condition but in ternary condition when i check whether "w==a" etc it does not check it..


how can i check such condition?

推荐答案

1。错误:a,b,c未定义



您应该在全局设置 int a,b,c



2.将参数 a,b,c,d 的值赋值给变量 a,b,c,d 您在全局设置了

1. ERROR : a,b,c are not defined

You should set int a,b,c in global

2.Assign value of the parameter a,b,c,d to variable a,b,c,d which you have set in the global
public Sub(int a, int b, int c, int d) {
      this.a=a;
      this.b=b;
      this.c=c;
      this.d=d;
   }



3.修改你的ocde,如下所示。 int 无法打印字符串结果




3. Modify your ocde as below. int cannot print String result

switch (k)
       {
           case 1:
               double c22 = w + x + y + z;
               if (w == 1 && x == 5 && y == 4 && z == 1) {
                   System.out.println("result for 1st");
               }
               else if(w == 1 && x == a && y == b && z == 1)
              {
               System.out.println("result for 2nd");
               }
               else if(w == c && x == a && y == b && z == 1)
               {
               System.out.println("result for 3nd");
               System.out.println("result for 4th");
               }
               else
               {
               System.out.println("no matched");
               }
           System.out.println("addition of two no. is :  " + c22);
           break;
           .....
        }


这篇关于如何检查以下代码中的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆