class :
main :
Code:
import java.util.List;
import java.awt.Event;
import java.util.ArrayList;
import java.util.Scanner;
public class InOut {
private static List<Integer> arrList;
private static List<String> arrList2;
private static Integer lim;
public static void stringEater() {
arrList2 = new ArrayList<>();
System.out.println("enter a string or finish");
Scanner scanny = new Scanner(System.in);
String in1 = scanny.next();
while(!(in1.equals("finish"))) {arrList2.add(in1);System.out.println("enter a string or finish");
in1 = scanny.next();}
}
public static void listcontains() {
System.out.println("enter a string to search");
Scanner scanny = new Scanner(System.in);
String in1 = scanny.next();
String result = arrList2.contains(in1)?"yes":"no"; // short conditional ? jutsu
System.out.println(result);
}
public static void evenUnEvens() {
// evens :
System.out.println("Even nums:");
for (Integer num : arrList) {
if(num%2==0) {System.out.print(" " + num);}
}
System.out.println();
System.out.println("unEven nums:");
for (Integer num : arrList) {
if(num%2!=0) {System.out.print(" " + num);}
}
}
public static void limInput(int amount) {
arrList = new ArrayList<>();
lim = amount;
System.out.println("get ready to enter " + amount + "numbers");
Scanner scanny = new Scanner(System.in);
for (int i = 0; i < amount; i++) {
System.out.println("enter number " + (amount - i) +" numbers to go");
arrList.add(scanny.nextInt());
}
}
public static int sum() {
int result=0;
for (Integer element : arrList) {
result += element;
}
return result;
}
public static double avg() {
int result=0;
for (Integer element : arrList) {
result += element;
}
return (double)result / (double)lim;
}
public static double choser() {
int result=0;
Scanner scanny = new Scanner(System.in);
System.out.println("choose sum or avg");
switch (scanny.next()) {
case "sum":
return sum();
case "avg":
return avg();
default:
break;
}
return result;
}
}
main :
Code:
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
InOut.limInput(10);
System.out.println(InOut.choser());
InOut.stringEater();
InOut.listcontains();
InOut.limInput(13);
InOut.evenUnEvens();
}
}