2010-09-20, 17:59
#613
int sum = 0;
for (int i=0; i<GGR; i++) {
y.kasta();
if(y.arYatzy())
sum++;
}
import java.io.*;
import java.util.*;
public class calc
{
public static String Calc (String op1, String op, String op2)
{
double tal1 = Double.parseDouble(op1);
double tal2 = Double.parseDouble(op2);
return tal1 + op + tal2 ;
}
public static void Calc(double op1, String op, double op2)
{
double tal1 = op1 ;
double tal2 = op2;
double summan1 = tal1 + tal2;
double summan2 = tal1 - tal2;
double summan3 = tal1 * tal2;
double summan4 = tal1 / tal2;
double summan5 = tal1 % tal2;
if (op.equals("+") )
{
System.out.print(tal1 + " + " + tal2 + " = " +(summan1));
}
else if ( op.equals("-"))
{
System.out.print(tal1 + " - " + tal2 + " = " +(summan2));
}
else if (op.equals("x"))
{
System.out.print(tal1 + " * " + tal2 + " = " +(summan3));
}
else if (op.equals("*"))
{
System.out.print(tal1 + " * " + tal2 + " = " +(summan3));
}
else if (op.equals("/") && tal1 != 0 && tal2 != 0)
{
System.out.print(tal1 + " / " + tal2 + " = " +(summan4));
}
else if(op.equals("/") && tal1 == 0)
{
System.out.print("Division med noll! \n");
}
else if(op.equals("/") && tal2 == 0)
{
System.out.print("Division med noll! \n");
}
else if(op.equals("%"))
{
System.out.print( tal1 + " % " + tal2 + " = " +(summan5));
}
else if (!op.equals("+")|| !op.equals("-")|| !op.equals("*")|| !
op.equals("x")|| !op.equals("/")|| !op.equals("%"))
{
System.out.println("Icke giltig operator: " + op + "\n");
System.out.println("Följande operatorer är giltiga:");
System.out.println("+ addition" + "\n" + "- subtraktion");
System.out.println("* multiplikation" + "\n"+"/ division");
System.out.println("% modulus" + "\n");
}
}
public static void main(String [] args) throws IOException
{
try
{
double tal1 = Double.parseDouble(args[0]);
String op = args[1];
double tal2 = Double.parseDouble(args[2]);
if ( args.length >= 4)
{
System.out.println("Användning: java Calc tal1 räknesätt tal2\n");
System.out.println("tal1 - ett tal");
System.out.println("räknesätt - +-x/%");
System.out.println("tal2 - ett tal\n");
System.out.println("Exempel: java calc 43.2 + 22.01");
}
calc.Calc(tal1, op, tal2);
}
catch (Exception e)
{
System.out.println("Användning: java Calc tal1 räknesätt tal2\n");
System.out.println("tal1 - ett tal");
System.out.println("räknesätt - +-x/%");
System.out.println("tal2 - ett tal\n");
System.out.println("Exempel: java Calc 43.2 + 22.01");
}
}
}
public static double Calc (String op1, String op, String op2) {
double tal1 = Double.parseDouble(op1);
double tal2 = Double.parseDouble(op2);
return Calc(tal1, op, tal2);
}
public static double Calc(double op1, String op, double op2) {
if(op.equals("+")){
return tal1 + tal2;
}
// etc, för alla räknesätt
}
import java.util.*;
public class Yatzy {
int[] tärningar = new int[5];
public void kasta()
{
System.out.print("Kasta tärningar");
System.out.println(" ");
for (int i= 0; i < tärningar.length; i++){
tärningar[i] = (int)(Math.random()*(7 -1) + 1) ;
System.out.print(" ");
System.out.print(tärningar[i]);
}
System.out.println(" ");
System.out.println(" ");
}
public void kasta(boolean tarning1, boolean tarning2, boolean tarning3, boolean tarning4, boolean tarning5 )
{
System.out.println("Kasta men spara träningar");
boolean[] Tarning = new boolean[5];
for (int j= 0; j < tärningar.length; j++){
Tarning[0] = tarning1;
Tarning[1] = tarning2;
Tarning[2] = tarning3;
Tarning[3] = tarning4;
Tarning[4] = tarning5;
if (tarning1|| tarning2|| tarning3|| tarning4|| tarning5 )
System.out.print(tärningar[j] = (int)(Math.random()*(7 -1) + 1));
System.out.print(" ");
}
}
public void sortera()
{
System.out.println(" ");
System.out.println(" ");
System.out.print("sortera ");
System.out.println(" ");
{
for (int i = 0; i < tärningar.length ; i++)
{
// Sätter startgissning för minsta talet till index i
int maxIndex = i;
for (int j = i + 1; j < tärningar.length; j++)
{
// Är talet mindre får vi nytt minIndex
if (tärningar[j] > tärningar[maxIndex])
maxIndex = j;
}
int tmp = tärningar[i];
tärningar[i] = tärningar[maxIndex];
tärningar[maxIndex] = tmp;
System.out.print(tärningar[i] + " ");
}
}
}
public boolean arYatzy() {
System.out.println("kontrollerar Yatzy");
boolean isItYatzy = true;
for (int j = 0; j < tärningar.length - 1; j++)
isItYatzy = isItYatzy && tärningar[j] == tärningar[j + 1];
if (isItYatzy)
System.out.println("Yatzy!!!");
else {
for (int j = 0; j < tärningar.length; j++)
System.out.print(tärningar[j] + " ");
System.out.println("ger inte Yatzy!");
}
return isItYatzy;
}
public String toString() {
String u = "";
System.out.println(" ");
for (int i = 0; i < tärningar.length ; i++)
{
u += tärningar[i] + " ";
}
return u;
}
}
public class YatzyTest3 {
private final static int GGR = 10000;
public static void main(String[] args) {
System.out.print("Sortering med SelectionSort tog... ");
long start = System.currentTimeMillis();
int x = 0 ;
for (int i=0; i<GGR; i++){
int[] tärningar = new int[5];
for (int h= 0; i < tärningar.length; i++){
tärningar[h] = (int)(Math.random()*(7 -1) + 1) ;
}
boolean isItYatzy = true;
for (int j = 0; j < tärningar.length - 1; j++)
isItYatzy = isItYatzy && tärningar[j] == tärningar[j + 1];
if (isItYatzy)
isItYatzy = true;
else {
for (int j = 0; j < tärningar.length; j++)
isItYatzy = false;
}
x++;
}
long tid = System.currentTimeMillis() - start;
System.out.print(tid + " ms");
System.out.println(" ");
System.out.println("Du fick Yatzy " + x + " gånger vilket är " + ((float)x) *x / 1296 + " procent");
}
}
)
public class YatzyTest3 {
public static void main(String[] args) {
double l = 100;
Yatzy2 y = new Yatzy2();
int[] tärningar = new int[1000];
System.out.print("Kasta tärningarna... ");
long start = System.currentTimeMillis();
int x = 0;
y.kasta();
x+=y.isYatzy()?1:0;
for (int i=0; i<tärningar.length; i++) {
x++;
}
long tid = System.currentTimeMillis() - start;
System.out.print(tid + " ms");
System.out.println(" ");
System.out.println("Du fick Yatzy " + x + " gånger vilket är " + ((float)x) *l/ 10000 + " %");
}
}
import java.util.Arrays;
public class SorteringTid
{
public void selectionSort(int[] a)
{
System.out.print("Kasta tärningarna... ");
long start = System.currentTimeMillis();
for (int i= 0; i < a.length; i++){
a[i] = (int)(Math.random()*(7 -1) + 1) ;
boolean isItYatzy = true;
for (int j = 0; j < a.length - 1; j++)
isItYatzy = isItYatzy && a[j] == a[j + 1];
if (isItYatzy)
System.out.println("Yatzy!!!");
else {
for (int j = 0; j < a.length; j++)
System.out.print(" ");
}
}
long tid = System.currentTimeMillis() - start;
System.out.println(tid + " ms");
}
public static void main(String[] args)
{
SorteringTid st = new SorteringTid();
double x= 0;
int[] selectionSortArray = new int[50];
for (int i = 0; i < selectionSortArray.length; i++)
{
selectionSortArray[i] = (int)(Math.random() * 10);
x++;
}
st.selectionSort(selectionSortArray);
System.out.println("Du fick Yatzy " + x + " gånger vilket är " + ((float)x) / 10000 + " %");
}
}
Du måste vara medlem för att kunna kommentera
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106