Execute HQ9+/Java: Difference between revisions
Content added Content deleted
m (Lowercase-ing input) |
m (Fixed syntax highlighting.) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{implementation|HQ9+}}{{collection|RCHQ9+}} |
{{implementation|HQ9+}}{{collection|RCHQ9+}} |
||
This [[Java]] program accepts input from a file named by its first argument, or std in if no arguments are given. |
|||
⚫ | |||
Note: A [[Unicode]] trick could be done to make this source code a quine (similar to the one done in [[RCHQ9+/Tcl]], but also replacing the literal 99's with "100 - 1"). |
|||
⚫ | |||
import java.io.FileReader; |
import java.io.FileReader; |
||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
Line 20: | Line 23: | ||
for(char instr:code.toCharArray()){ |
for(char instr:code.toCharArray()){ |
||
switch(instr){ |
switch(instr){ |
||
case 'q': System.out.println(code);break; |
case 'q': System.out.println(code); break; |
||
case 'h': System.out.println("Hello, World!");break; |
case 'h': System.out.println("Hello, World!"); break; |
||
case '9': printBottles();break; |
case '9': printBottles(); break; |
||
case '+': acc++;break; |
case '+': acc++; break; |
||
default: //ignore other chars |
default: //ignore other chars |
||
} |
} |
||
Line 44: | Line 47: | ||
"1#One bottle|2#{0} bottles} of beer", n); |
"1#One bottle|2#{0} bottles} of beer", n); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
Latest revision as of 10:16, 1 September 2022
Execute HQ9+/Java is an implementation of HQ9+.
Other implementations of HQ9+.
Execute HQ9+/Java is part of RCHQ9+. You may find other members of RCHQ9+ at Category:RCHQ9+.
This Java program accepts input from a file named by its first argument, or std in if no arguments are given.
Note: A Unicode trick could be done to make this source code a quine (similar to the one done in RCHQ9+/Tcl, but also replacing the literal 99's with "100 - 1").
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.text.MessageFormat;
public class HQ9p{
public static void main(String[] args) throws Exception{
int acc = 0;
String code = "";
BufferedReader input;
if(args.length > 0){
input = new BufferedReader(new FileReader(args[0]));
}else{
input = new BufferedReader(new InputStreamReader(System.in));
}
while(input.ready()){
code += input.readLine().toLowerCase();
}
for(char instr:code.toCharArray()){
switch(instr){
case 'q': System.out.println(code); break;
case 'h': System.out.println("Hello, World!"); break;
case '9': printBottles(); break;
case '+': acc++; break;
default: //ignore other chars
}
}
}
public static void printBottles(){
String byob = bottles(99);
for (int x = 99; x > 0;) {
System.out.println(byob + " on the wall");
System.out.println(byob);
System.out.println("Take one down, pass it around");
byob = bottles(--x);
System.out.println(byob + " on the wall\n");
}
}
static String bottles(int n){
return MessageFormat.format("{0,choice,0#No more bottles|" +
"1#One bottle|2#{0} bottles} of beer", n);
}
}