voir aussi 5.2 "Les types de données" [22] |
||
http://tecfa.unige.ch/guides/java/staf2x/ex/basics/SimpleCalculs.java
// public class SimpleCalculs { // All programs must have main() public static void main(String[] args) { int somme = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; float moyenne = somme / 10; System.out.println ("La moyenne est égale à " + moyenne); } }
http://tecfa.unige.ch/guides/java/staf2x/ex/basics/ForYou.java
public class ForYou { // All programs must have main() public static void main(String[] args) { // Say hello! for (int i=1; i<11; i++) { System.out.println("J'aimerai t'embrasser " + i + " fois\n "); } } }
(nombre > 1) |
||
{instruction; instruction; ....} ou instruction_simple; |
{ a = 1 + a; b = 2 + a; } |
|
for (int i=1) { System.out.println ("forever"); } |
||
import java.io.*; class Condition { /* A simple program to say what is the shortest string of two * -------------------------- Java 1.1 Oct 1998 */ public static void main (String [] args) throws IOException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please: input a string"); String thefirststring = in.readLine(); System.out.println("Please: input another string"); String thesecondstring = in.readLine(); if (thesecondstring.length() < thefirststring.length()) {System.out.println("The second string is shorter"); } else { if (thesecondstring.length() == thefirststring.length()) { System.out.println("The two strings have the same length"); } else { System.out.println("The first string is shorter"); } } } }
http://tecfa.unige.ch/guides/java/staf2x/ex/basics/Sign.java
import java.io.*; class Sign { /* A simple program to get the sign of a number * -------------------------- Java 1.1 Oct 1998 */ public static void main (String [] args) throws IOException, NumberFormatException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please: input a number"); Integer thenumber = new Integer(in.readLine()); if (thenumber.intValue() < 0) { System.out.println("Negative number"); } else { System.out.println("Positive number"); } } }
(voir 5.5 "La sélection (if/else) et les conditionnels" [25] |
||
a = 0; while (a < 10) { n = n + a; a = a + 1; } |
while (CurrentNumber.length()!=0) { TheSum = TheSum + (new Integer(CurrentNumber)).intValue(); CurrentNumber=in.readLine(); }
http://tecfa.unige.ch/guides/java/staf2x/ex/basics/Sum.java
import java.io.*; class Sum { /* A simple program to get the sum of a list of numbers * -------------------------- Java 1.1 Oct 1998 */ public static void main (String [] args) throws IOException, NumberFormatException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please:"); System.out.println("Input a list of numbers, pressing enter between each number."); System.out.println("When you have finished, just press enter."); int TheSum = 0; String CurrentNumber = in.readLine(); while (CurrentNumber.length()!=0) { TheSum = TheSum + (new Integer(CurrentNumber)).intValue(); CurrentNumber=in.readLine(); } System.out.println("The sum of all the numbers is " + TheSum); } }
catch (Signal_d'exception var) { |
||
try { Integer TheNumber = new Integer(in.readLine()); System.out.println("The string you typed is a valid java number."); } catch (NumberFormatException e) { System.out.println("The string you typed is not a valid java number."); }
http://tecfa.unige.ch/guides/java/staf2x/ex/basics/Catch.java
import java.io.*; class Catch { /* A simple program to read a string and tell if it is a valid java number * -------------------------- Java 1.1 Oct 1998 */ public static void main (String [] args) throws IOException, NumberFormatException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please: Input a number."); try { Integer TheNumber = new Integer(in.readLine()); System.out.println("The string you typed is a valid java number."); } catch (NumberFormatException e) { System.out.println("The string you typed is not a valid java number."); } } }
import java.io.*; class Call { /* A simple program to show the way java methods are invoked * -------------------------- Java 1.1 Oct 1998 */ static String TheString; // constructor for the class Call () throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please:"); System.out.println("Input a string"); TheString = in.readLine(); System.out.println("The palindrome is:"); System.out.println(Palindromize1(TheString) + " (using method 1)"); System.out.println(Palindromize2(TheString) + " (using method 2)"); System.out.println("Hopefully, we get the same result"); } // Simple Main method public static void main (String [] args) throws IOException { Call TheCall = new Call(); } String Palindromize1 (String AString) { if (AString.length() < 2) { return AString; } else { // return the palindromized (2-last element) + 1st char return Palindromize1(AString.substring(1)).concat(AString.substring(0,1)); } } String Palindromize2 (String AString) { String ThePalindrome = new String(); for (int i = (AString.length() - 1); i >= 0; i--) { ThePalindrome = ThePalindrome.concat(AString.substring(i,i + 1)); } return ThePalindrome; } }
Définit une table avec 100 éléments, chaque élément doit être un nombre entier. |
http://tecfa.unige.ch/guides/java/staf2x/ex/basics/Table2.java
import java.io.*; class Table2 { /* A VERY simple program to show how to use an array * -------------------------- Java 1.1 Oct 1998 */ public static void main (String [] args) throws IOException, NumberFormatException { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please:"); System.out.println("Input a list of numbers, pressing enter between each number."); System.out.println("When you have finished, just press enter."); int TheTableLength = 0; int TheTable[] = new int[100]; String CurrentNumber = in.readLine(); while (CurrentNumber.length()!=0) { TheTableLength = TheTableLength + 1; TheTable[TheTableLength - 1] = (new Integer(CurrentNumber)).intValue(); CurrentNumber=in.readLine(); } System.out.println("The computer has a very big short term memory (100 elements max):"); for (int i = 0; i < (TheTableLength); i++) { System.out.print(TheTable[i] + " "); } } }