User:Binari: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 37: Line 37:
i=i/2;
i=i/2;
}
}
}
}
</lang>

==Loops/Nested==
<lang java>
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int i,j;
int[][] ar=new int[20][20];
Random rast=new Random();
for (i=0; i<20; i++) {
for (j=0; j<20; j++) {
ar[i][j]=rast.nextInt(20)+1;
}
}
System.out.println("R C");
for (i=0; i<20; i++) {
for (j=0; j<20; j++) {
if (ar[i][j]==20) {
System.out.println(i+" "+j);
break;
}
}
}
// your code goes here
}
}
</lang>

==Loops/N plus one half==
<lang java>
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
for (int i=1; i<=10; i++) {
System.out.print(i);
if (i!=10) System.out.print(", ");
else System.out.println();
}
// your code goes here
}
}
</lang>

==99 Bottles of Beer==
<lang java>
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
for (int i=99; i>=1; i--) {
System.out.println(i+" bottles of beer on the wall, "+i+" bottles of beer");
System.out.println("Take one down and pass it around, "+(i-1)+" bottles of beer on the wall.");
System.out.println(); //no grammar
}
}
}
</lang>

==HTTP==
<lang java>
import java.net.*;
import java.io.*;

class HTTP {
public static void main(String[] args) throws Exception {
URL url=new URL("http://emreperde.com");
BufferedReader in= new BufferedReader(new InputStreamReader(url.openStream()));
String oku;
while ((oku=in.readLine())!=null) {
System.out.println(oku);
}
in.close();
}
}
</lang>

==String matching==
<lang java>
import java.util.*;
import java.lang.*;

class matching {
public static void main(String[] args) {
String s1,s2;
String t1="no",t2="no",t3="no";
Scanner iste=new Scanner(System.in);
System.out.print("First string: ");
s1=iste.next();
System.out.print("Second string: ");
s2=iste.next();
if (s1.startsWith(s2)) { t1="yes"; t3="yes"; }
if (s1.endsWith(s2)) { t2="yes"; t3="yes"; }
if (s1.indexOf(s2)!=-1) t3="yes";
System.out.println("First string:");
System.out.println("starts with second string? "+t1);
System.out.println("contains the second string at any location? "+t3);
System.out.println("ends with second string? "+t2);
}
}
</lang>

==Roman numerals/Decode==
<lang java>
import java.util.*;
import java.lang.*;

class romaDecode {
public static void main(String[] args) {
int say=0;
Scanner iste=new Scanner(System.in);
System.out.print("Enter Roman numeral: ");
String yaz=iste.next();
for (int i=0; i<yaz.length(); i++) {
switch(yaz.charAt(i)) {
case 'M': say+=1000; break;
case 'L': say+=50; break;
case 'X':
if (i!=yaz.length()-1) {
if (yaz.charAt(i+1)=='L' || yaz.charAt(i+1)=='C') say-=10;
else say+=10; }
else say+=10;
break;
case 'I':
if (i!=yaz.length()-1) {
if (yaz.charAt(i+1)=='X' || yaz.charAt(i+1)=='V') say--;
else say++; }
else say++;
break;
case 'V': say+=5; break;
case 'C':
if (i!=yaz.length()-1) {
if (yaz.charAt(i+1)=='M') say-=100;
else say+=100; }
else say+=100;
break;
case 'D': say+=500; break;
default: say=0; break;
}
}
System.out.println("Result: "+say);
}
}
}
}

Latest revision as of 16:59, 1 August 2015

Hello World/Graphical

<lang java> import javax.swing.*;

class helloGUI { private helloGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame penc=new JFrame("Elveda"); penc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel yaz=new JLabel("Goodbye, World!"); penc.getContentPane().add(yaz); penc.pack(); penc.setVisible(true); } public static void main(String[] args) { new helloGUI(); } } </lang>

Hello world/Newline omission

<lang java> class noNewline { public static void main(String[] args) { System.out.print("Goodbye, World!"); } } </lang>

Loops/While

<lang java> class whileLoop { public static void main(String[] args) { int i=1024; while (i>0) { System.out.println(i); i=i/2; } } } </lang>

Loops/Nested

<lang java> /* package whatever; // don't place package name! */

import java.util.*; import java.lang.*; import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { int i,j; int[][] ar=new int[20][20]; Random rast=new Random(); for (i=0; i<20; i++) { for (j=0; j<20; j++) { ar[i][j]=rast.nextInt(20)+1; } } System.out.println("R C"); for (i=0; i<20; i++) { for (j=0; j<20; j++) { if (ar[i][j]==20) { System.out.println(i+" "+j); break; } } } // your code goes here } } </lang>

Loops/N plus one half

<lang java> /* package whatever; // don't place package name! */

import java.util.*; import java.lang.*; import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { for (int i=1; i<=10; i++) { System.out.print(i); if (i!=10) System.out.print(", "); else System.out.println(); } // your code goes here } } </lang>

99 Bottles of Beer

<lang java> /* package whatever; // don't place package name! */

import java.util.*; import java.lang.*; import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { for (int i=99; i>=1; i--) { System.out.println(i+" bottles of beer on the wall, "+i+" bottles of beer"); System.out.println("Take one down and pass it around, "+(i-1)+" bottles of beer on the wall."); System.out.println(); //no grammar } } } </lang>

HTTP

<lang java> import java.net.*; import java.io.*;

class HTTP { public static void main(String[] args) throws Exception { URL url=new URL("http://emreperde.com"); BufferedReader in= new BufferedReader(new InputStreamReader(url.openStream())); String oku; while ((oku=in.readLine())!=null) { System.out.println(oku); } in.close(); } } </lang>

String matching

<lang java> import java.util.*; import java.lang.*;

class matching { public static void main(String[] args) { String s1,s2; String t1="no",t2="no",t3="no"; Scanner iste=new Scanner(System.in); System.out.print("First string: "); s1=iste.next(); System.out.print("Second string: "); s2=iste.next(); if (s1.startsWith(s2)) { t1="yes"; t3="yes"; } if (s1.endsWith(s2)) { t2="yes"; t3="yes"; } if (s1.indexOf(s2)!=-1) t3="yes"; System.out.println("First string:"); System.out.println("starts with second string? "+t1); System.out.println("contains the second string at any location? "+t3); System.out.println("ends with second string? "+t2); } } </lang>

Roman numerals/Decode

<lang java> import java.util.*; import java.lang.*;

class romaDecode { public static void main(String[] args) { int say=0; Scanner iste=new Scanner(System.in); System.out.print("Enter Roman numeral: "); String yaz=iste.next(); for (int i=0; i<yaz.length(); i++) { switch(yaz.charAt(i)) { case 'M': say+=1000; break; case 'L': say+=50; break; case 'X': if (i!=yaz.length()-1) { if (yaz.charAt(i+1)=='L' || yaz.charAt(i+1)=='C') say-=10; else say+=10; } else say+=10; break; case 'I': if (i!=yaz.length()-1) { if (yaz.charAt(i+1)=='X' || yaz.charAt(i+1)=='V') say--; else say++; } else say++; break; case 'V': say+=5; break; case 'C': if (i!=yaz.length()-1) { if (yaz.charAt(i+1)=='M') say-=100; else say+=100; } else say+=100; break; case 'D': say+=500; break; default: say=0; break; } } System.out.println("Result: "+say); } } </lang>