samedi 3 octobre 2020

branches / listing names

Hey i am having some trouble on the following assignment. Any help would be very welcomed. I keep getting this error and I think it has something to do with the file/class name but im not sure.

ProfNames.java:3: error: '{' expected public class ProfNames.java { ^

A university has a web page that displays the instructors for a course, using the following algorithm: If only one instructor exists, the instructor's first initial and last name are listed. If two instructors exist, only their last names are listed, separated by /. If three exist, only the first two are listed, with "/ …" following the second. If none exist, print "TBD". Given six words representing three first and last names (each name a single word; latter names may be "none none"), output one line of text listing the instructors' names using the algorithm. If the input is "Ann Jones none none none none", the output is "A. Jones". If the input is "Ann Jones Mike Smith Lee Nguyen" then the output is "Jones / Smith / …".

Hints:

Use an if-else statement with four branches. The first detects the situation of no instructors. The second one instructor. Etc.

Detect whether an instructor exists by checking if the first name is "none".





import java.util.Scanner;

public class ProfNames.java {
public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);

  firstName1 = scnr.next();
  lastName1 = scnr.next();
  firstName2 = scnr.next();
  lastName2  = scnr.next();
  firstName3 = scnr.next();
  lastName3 = scnr.next();
  
  
  if (firstName1.equals ("none")) {
     System.out.println ( "TBD");
  }
  
  else if (firstName2.equals ("none")) {
     System.out.println(firstName1.charAt(0) + " " + lastname1);
  }
  
  else if (firstName3.equals ("none")) {
     System.out.println (lastName1 + " / " + lastname2);
  }
  
     else { System.out.println (lastName1 + "/" + lastName2 + "/...");
     
     }
  
  

} }

Aucun commentaire:

Enregistrer un commentaire