【问题标题】:Highest Value among 3 Numbers3 个数字中的最大值
【发布时间】:2020-04-08 10:03:26
【问题描述】:

问题陈述:SRV 学院希望认可本学年成功获得最多实习机会的院系。参与招聘的部门有CSE、ECE、MECH。帮助学院找到获得最大展示位置的部门。检查示例快照中给出的所有可能输出

注意:如果任何输入为负,则输出应为“输入无效”。如果所有部门的职位数量相同,则输出应为“没有一个部门获得最高职位”。

示例输入 1:

Enter the no of students placed in CSE:90
Enter the no of students placed in ECE:45
Enter the no of students placed in MECH:70
Sample Output 1:
Highest placement
CSE

示例输入 2:

Enter the no of students placed in CSE:55
Enter the no of students placed in ECE:85
Enter the no of students placed in MECH:85
Sample Output 2:
Highest placement
ECE
MECH

示例输入 3:

Enter the no of students placed in CSE:0
Enter the no of students placed in ECE:0
Enter the no of students placed in MECH:0
Sample Output 3:
None of the department has got the highest placement

示例输入 4:

Enter the no of students placed in CSE:10
Enter the no of students placed in ECE:-50
Enter the no of students placed in MECH:40
Sample Output 3:
Input is Invalid 

我能够提出一个基本上通过所有可见条件的代码,但未能测试一个。不太确定我哪里出错了。

我完全同意下面的代码效率不高

import java.util.*;
class Placement{
public static void main(String[] args) {
int x,y ,z ;
scanner sc= new Scanner(System.in) ;
System.out.printIn( "Enter the no of students placed in CSE: " );
x=sc.nextInt( );
System.out.printIn("Enter the no of students placed in ECE:" );
y=sc.nextInt( ) ;
System.out.println("Enter the no of students placed in MECH:" );
z=sc.nextInt( ) ;
if(x==z && x==y && y==z){   
System.out.printIn("None of the department has got the highest placement" ) ;
}
else if(x<0 || y<0 || z<0){
System.out.printIn("Input is Invalid" ) ;
}
else{
System.out.println( "Highest placement" );
if(x>=z && x>=z){
if(x==y){
System.out.printIn( "CSE" );
System.out.printIn("ISE" );
}
else if(x==z) {
System.out.printIn("CSE" ) ;
System.out.printIn( "MECH" ) ;
}
else{
System.out.println("CSE" );
}
}
else if(y>=z){
{
if(y==z){
System.out.printIn( "ECE" );
System.out.printIn( "MECH" ) ;
}
else{
System.out.println("ECE" ) ;
}
else{
System.out.printIn( "MECH" ) ;
}
}
}

输出:

Proposed grade: 85. 71 / 100
Result Description
Failed tests
Test 2: CheckConditionsForTwoMax
Test 1: Check Conditions

Summary of tests
*Note: All the test cases might not have same weightage
7 tests run/ 6 tests passed

【问题讨论】:

  • 请在问题本身中包含所有代码和输出
  • 问题是我正在编码的平台不允许我复制或粘贴代码
  • @Cruncher 我已经通过取出图像添加了代码。财政年度
  • 抱歉,我通过添加代码图像让问题不清楚,我能够从图像中提取文本并添加代码 sn-p 以及获得的输出。
  • 您发布的代码将无法编译:它有System.out.printIn(最后一个字符之前是大写i,而不是小写L)。在单个 if 语句之后还有 2 个 else 块。

标签: java data-structures


【解决方案1】:

很好地找出了错误,不是有效的答案,但得到了它的工作。

import java.util.*;
class Placement{
public static void main(String[ ] args){
int a, b, c;
Scanner sc= new Scanner(System. in) ;
System.out.println("Enter the no of students placed in CSE:");
a=sc.nextInt();
System.out.println("Enter the no of students placed in ECE:");
b=sc.nextInt();
System.out.println("Enter the no of students placed in MECH:");
c=sc.nextInt();
if (a>b && a>c){
System.out.println("Highest placement" ) ;
System.out.println("CSE");
}
else if(a<0 || b<0 || c<0){
System.out.println("Input is Invalid");
}
else if(b>a && b>c){
System.out.println("Highest placement") ;
System.out.println("ECE") ;
}
else if(c>a && c>b){
System.out.println("Highest placement") ;
System.out.println( "MECH" ) ;
}
else if(a==b && b==c && c==a){
System.out.println( "None of the department has got the highest placement" ) ;
}
else if(a==b){
System.out.println("Highest placement");
System.out.println("CSE");
System.out.println("ECE");
}
else if(a==c){
System.out.println("Highest placement");
System.out.println("CSE") ;
System.out.println("MECH") ;
}
else{
System.out.println("Highest placement");
System.out.println("ECE") ;
System.out.println("MECH") ;
}
}
}

【讨论】:

    【解决方案2】:

    唯一的错误是您在 a==b 条件下输入了 ICE 而不是 ECE

    【讨论】:

      【解决方案3】:
       import java.io.*;
       import java.util.*;
       public class Placement{
           public static void main (String[] args) {
               int ece,cse,mech;
               Scanner sc=new Scanner(System.in);
               System.out.println("Enter the no of students placed in CSE:");
               cse=sc.nextInt();
                       
               System.out.println("Enter the no of students placed in ECE:");
               ece=sc.nextInt();
                      
               System.out.println("Enter the no of students placed in MECH:");
               mech=sc.nextInt();
               sc.close();
                      
               if(ece==cse&&ece==mech)
                   System.out.println("None of the departments has got the highest placement");
               else if (ece<0||cse<0||mech<0)
                   System.out.println("Input is Invalid");
               else
               {
                   System.out.println("Highest placement");
                   if((cse>=ece)&&(cse>=mech))
                       if(cse==ece)
                            System.out.println("CSE"+"\nECE");
                       else if (cse==mech)
                           System.out.println("CSE\n"+"MECH");
                       else
                           System.out.println("CSE");
                   else if (ece>=mech&&ece>=cse)
                       if(ece==mech)
                           System.out.println("ECE\n"+"MECH");
                       else if(ece==cse)
                           System.out.println("ECE\n"+"CSE");
                       else
                           System.out.println("ECE");
                   else
                           System.out.println("MECH");
               }
           }
       }
      

      *评估报告 [-]失败的测试 测试 4:检查 No-Highest-Placement 测试 6:检查 No-Highest-Placement [-]测试1:检查条件

      [-]测试摘要 *注意:所有测试用例的权重可能不同

      | 7 个测试运行/5 个测试通过 |

      *

      【讨论】:

        【解决方案4】:
        import java.util.*;
        
        class Placement
        {
            public static void main (String[] args)
            {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter the no of students placed in CSE:");
                int cse = sc.nextInt();
                System.out.println("Enter the no of students placed in ECE:");
                int ece = sc.nextInt();
                System.out.println("Enter the no of students placed in MECH:");
                int mech = sc.nextInt();
                
                if(cse < 0 || ece < 0 || mech < 0) 
                    System.out.print("Input is Invalid");
                else if(cse == ece && ece == mech) 
                    System.out.print("None of the department has got the highest placement");
                else if (cse == ece && cse > mech)
                    System.out.print("Highest placement\n CSE\n ECE");
                else if (ece == mech && cse < ece)
                    System.out.print("Highest placement\n ECE\n MECH");
                else if (cse == mech && ece < mech)
                    System.out.print("Highest placement\n MECH\n CSE");
                else if (cse > ece && cse > mech)
                    System.out.print("Highest placement\n CSE");
                else if (ece > cse && ece > mech)
                    System.out.print("Highest placement\n ECE");
                else if (mech > cse && mech > ece)
                    System.out.print("Highest placement\n MECH");
            }
        }
        

        这将完美运行。

        【讨论】:

          【解决方案5】:

          代码需要检查放置数量是否为零

          import java.util.*;
          public class Placement {
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              System.out.printf("Enter the no of students placed in CSE:");
              String str1 = sc.nextLine();
              int i1 = Integer.parseInt(str1);
              System.out.printf("Enter the no of students placed in ECE:");
              String str2 = sc.nextLine();
              int i2 = Integer.parseInt(str2);
              System.out.printf("Enter the no of students placed in MECH:");
              String str3 = sc.nextLine();
              int i3 = Integer.parseInt(str3);
              if (i1 == 0 && i2 == 0 && i3 == 0) {
                  System.out.println("None of the department has got the highest placement");
              } else if (i1 == i2 && i2 == i3 && i3 == i2) {
                  System.out.println("None of the department has got the highest placement");
              } else if (i1 < 0 || i2 < 0 || i3 < 0) {
                  System.out.println("Input is Invalid");
              } else if (i1 == i2 && i1 != 0) {
                  System.out.println("Highest placement");
                  System.out.println("CSE");
                  System.out.println("ECE");
              } else if (i1 == i3 && i1 != 0) {
                  System.out.println("Highest placement");
                  System.out.println("CSE");
                  System.out.println("MECH");
              } else if (i2 == i3 && i2 != 0) {
                  System.out.println("Highest placement");
                  System.out.println("ECE");
                  System.out.println("MECH");
              } else if (i1 > i2) {
                  if (i1 > i3) {
                      System.out.println("Highest placement");
                      System.out.println("CSE");
                  } else if (i1 == i3) {
                      System.out.println("Highest placement");
                      System.out.println("CSE");
                      System.out.println("MECH");
                  } else {
                      System.out.println("Highest placement");
                      System.out.println("MECH");
                  }
              } else if (i2 > i1) {
                  if (i2 > i3) {
                      System.out.println("Highest placement");
                      System.out.println("ECE");
                  } else if (i2 == i3) {
                      System.out.println("Highest placement");
                      System.out.println("ECE");
                      System.out.println("MECH");
                  } else {
                      System.out.println("Highest placement");
                      System.out.println("MECH");
                  }
              } else if (i1 != 0 && i2 != 0 && i3 != 0 && i1 == i2 && i2 == i3) {
                  System.out.println("Highest placement");
                  System.out.println("CSE");
                  System.out.println("ECE");
                  System.out.println("MECH");
              } else {
                  System.out.println("MECH");
              }
          
          }
          

          【讨论】:

            【解决方案6】:
            import java.util.Scanner;
            public class Placement {
            public static void main (String[] args) {
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter the no of students placed in CSE:");
                    int CSE= sc.nextInt();
                System.out.println("Enter the no of students placed in ECE:");
                    int ECE=sc.nextInt();
                System.out.println("Enter the no of students placed in MECH:");
                    int MECH=sc.nextInt();
                    
                if(CSE<0||ECE<0||MECH<0)
                {
                    System.out.println("Input is Invalid");
                }
                
                else if(CSE==ECE && ECE==MECH)
                {
                    System.out.println("None of the department has got the highest placement");
                }
                
                else if(CSE==ECE && MECH<CSE)
                {
                    System.out.println("Highest placement");
                    System.out.println("CSE");
                    System.out.println("ECE");
                }
                
                else if(CSE==MECH && ECE<MECH)
                {
                    System.out.println("Highest placement");
                    System.out.println("CSE");
                    System.out.println("MECH");
                }
                
                else if(ECE==MECH && CSE<MECH)
                {
                    System.out.println("Highest placement");
                    System.out.println("ECE");
                    System.out.println("MECH");
                }
                
                else if(CSE>ECE&&CSE>MECH)
                {
                    System.out.println("Highest placement");
                    System.out.println("CSE");
                }
                
                else if(ECE>CSE && ECE>MECH)
                {
                    System.out.println("Highest placement");
                    System.out.println("ECE");
                }
                
                else if(MECH>CSE&&MECH>ECE)
                {
                    System.out.println("Highest placement");
                    System.out.println("MECH");
                }
              }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-10-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多