Jump to content

Help checking the java code

- - - - -

  • Please log in to reply
4 replies to this topic

#1
doggy2007

doggy2007

    Newbie

  • Members
  • Pip
  • 2 posts
I dont know if i do wrong please check and correct my code :)



import java.util.Scanner;


public class Ex02 {
public static void main(String [] args){
String nom, prénom, sex, général, math, physique, chimie, A;
write("Nom: ");
nom=readString();
write("Prénom: ");
prénom=readString();
write("Mention générale: ");
général=readString();
write("Mention mathématique: ");
math=readString();
write("Mention physique: ");
physique=readString();
write("Mention chimie: ");
chimie=readString();
if ((général=="A" ||général== "B" || général== "C" || général== "D") && (math=="A" ||math== "B" || math== "C" || math== "D") && (physique=="A" ||physique== "B" || physique== "C" || physique== "D") && (chimie=="A" ||chimie== "B" || chimie== "C" || chimie== "D") ){
write("M. " +nom+ " est permis de faire le concours d'entrée pour la formation d'ingénieur à l'ITC.");
}

}


else if (général=="E" && math=="E" && physique== "E" && chimie=="E" ){
write("M. " +nom+ " n'est pas permis de faire le concours d'entrée pour la formation d'ingénieur à l'ITC.");

}





/* Ne touchez pas les codes au-dessous */
public static void write(int out){
System.out.print(out);
}
public static void write(double out){
System.out.print(out);
}
public static void write(String out){
System.out.print(out);
}
public static int readInt(){
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}
public static double readDouble(){
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
return d;
}
public static String readString(){
Scanner sc = new Scanner(System.in);
String s = sc.next();
return s;
}
}

#2
Eieio

Eieio

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Well first off your code is not spaced good and looks like crap. So fix that. Its not hard, just read the article below.

Good place to start reading: Top 15+ Best Practices for Writing Super Readable Code | Nettuts+

^^ Above will show you the ropes on making your code readable.

On top of that I can't read your language. What is the point of the program?

You initialized your variables wrong. It should look like the following:


String myName = "Eieio";

short myAge = 19;

Color favoriteColor = Color.RED;


Are you just trying to learn to use scanner? Because if so I can give you a challenge for you to try to complete rather than just writing some random code to test it out.

Ok, your code sucks man. Why would you basically re-write one of the System's? It really made me think you were writing to the disk... or maybe you are... its kind of a mind ****. Either way write("Nom: "); should be System.out.println("Nonm: ");.

Also for getting user input you should have used System.in.read();. Of course that reads one byte at a time. So I can see where you may want to use the Scanner method instead.

#3
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Nah,don't mind the spaces - use netbeans and it will place the needed spaces for you. Keep trying and never mind what other people are thinking about your codes. ;)

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Next time, when posting code, please put the code in between
 [ /code] tags. This makes it much easier for people like me to read.


Example:

[code]

public void a() { 

    doSomething();

}


doggy2007 said:

I dont know if i do wrong please check and correct my code :)
Does the program compile? If not, what error do you receive?

Also, if we do not know what the program is suppose to do, how can we answer if it is wrong?

From a quick glance, it looks like you're trying to pull values from the user, and then display the data at the end of the program.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The program will fail due to using scanner.next() instead of nextLine().




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users