Skip to main content
Commonmark migration
Source Link

I felt the need to do Java some justice. :)

#Java - 486 437 421 414

Java - 486 437 421 414

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:f+f/2;if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:f+f/2;
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}

I felt the need to do Java some justice. :)

#Java - 486 437 421 414

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:f+f/2;if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:f+f/2;
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}

I felt the need to do Java some justice. :)

Java - 486 437 421 414

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:f+f/2;if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:f+f/2;
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}
deleted 3 characters in body
Source Link
asteri
  • 924
  • 8
  • 16

I felt the need to do Java some justice. :)

#Java - 486 437 421421 414

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:(int)(f*1.5);iff+f/2;if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:(int)(f*1.5);f+f/2;
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}

I felt the need to do Java some justice. :)

#Java - 486 437 421

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:(int)(f*1.5);if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:(int)(f*1.5);
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}

I felt the need to do Java some justice. :)

#Java - 486 437 421 414

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:f+f/2;if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:f+f/2;
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}
deleted 30 characters in body
Source Link
asteri
  • 924
  • 8
  • 16

I felt the need to do Java some justice. :)

#Java - 486 437 426421

Golfed

import java.util.*;class aGuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,h=0,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():h==0f>g?f/2:(int)(f*1.5);if(c==1)p(f);h=f>g?0:1;p;p(h==0f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,h=0,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():h==0f>g?f/2:(int)(f*1.5);
            if(c==1)p(f);
            h=f>g?0:1;
            p(h==0f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}

I felt the need to do Java some justice. :)

#Java - 486 437 426

Golfed

import java.util.*;class a{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,h=0,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():h==0?f/2:(int)(f*1.5);if(c==1)p(f);h=f>g?0:1;p(h==0?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,h=0,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():h==0?f/2:(int)(f*1.5);
            if(c==1)p(f);
            h=f>g?0:1;
            p(h==0?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}

I felt the need to do Java some justice. :)

#Java - 486 437 421

Golfed

import java.util.*;class GuessANumber{static<T>void p(T p){System.out.println(p);}static int i(){return new Scanner(System.in).nextInt();}public static void main(String[]a){int c,g,f=101,i=0;p("Pick (1) or guess (2)?");c=i();if(c==1)p("Pick a number (1-100)");g=c==1?i():new Random().nextInt(100);while(f!=g&&i++<5){p("Guess:");f=c==2?i():f>g?f/2:(int)(f*1.5);if(c==1)p(f);p(f>g?"lower":"higher");}if(f==g)p("correct");}}

Slightly Ungolfed

import java.util.*;

class a{
    
    static<T>void p(T p){
        System.out.println(p);
    }
    
    static int i(){
        return new Scanner(System.in).nextInt();
    }
    
    public static void main(String[]a){
        int c,g,f=101,i=0;
        p("Pick (1) or guess (2)?");
        c=i();
        if(c==1)p("Pick a number (1-100)");
        g=c==1?i():new Random().nextInt(100);
        while(f!=g&&i++<5){
            p("Guess:");
            f=c==2?i():f>g?f/2:(int)(f*1.5);
            if(c==1)p(f);
            p(f>g?"lower":"higher");
        }
        if(f==g)p("correct");
    }
}
deleted 19 characters in body
Source Link
asteri
  • 924
  • 8
  • 16
Loading
edited body
Source Link
asteri
  • 924
  • 8
  • 16
Loading
deleted 202 characters in body
Source Link
asteri
  • 924
  • 8
  • 16
Loading
Source Link
asteri
  • 924
  • 8
  • 16
Loading