I want to combine below two for loops and corresponding if-loops with break statement. How can I do that.
public class Test {
public static void main(String[] args) {
for(int i=0;i<10;i++){
if(i==2){
System.out.println("i2: "+ i);
break;
}
System.out.println("i2: "+ i);
}
for(int i=0;i<10;i++){
if(i==4){
System.out.println("i4: "+ i);
break;
}
System.out.println("i4: "+ i);
}
}
}
Try this
public class Test {
public static void main(String[] args) {
boolean 2completed = false;
boolean 4completed = false;
for(int i=0;i<10;i++){
if(i==2){
System.out.println("i2: "+ i);
2completed = true;
}
else if(i==4){
System.out.println("i4: "+ i);
4completed = true
}
if(!2completed)
System.out.println("i2: "+ i);
if(!4completed)
System.out.println("i4: "+ i);
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
Improve this question
The code works on a linkedlist principle, and has worked alright thus far, however the code outputs an error when input is add, 1, a, 10, 5, add, 1, b, 2, 2, add, 2, b, 2, 1, inc, 2, b, print, done
the code:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
class Prece {
private String nosaukums;
private double cena;
private int daudzums;
public Prece(String n, double c, int d) {
nosaukums = n;
cena = c;
daudzums = d;
}
public String getNosaukums() {
return nosaukums;
}
public double getCena() {
return cena;
}
public int getDaudzums() {
return daudzums;
}
public void setDaudzums(int d) {
daudzums = d;
}
public static Prece inputPrece(Scanner sc) {
String n = sc.next();
double c = sc.nextDouble();
int d = sc.nextInt();
return new Prece(n, c, d);
}
public void outputPrece() {
System.out.printf("%-20s%-10.2f%-10d\n", nosaukums, cena, daudzums);
}
}
public class Main {
public static Scanner sc;
public static void main(String[] args) {
HashMap<String, LinkedList<Prece>> pasutijumi = new HashMap<String, LinkedList<Prece>>();
sc = new Scanner(System.in);
String cmd = "";
while (!cmd.equals("done")) {
cmd = sc.next();
switch (cmd) {
case "add":
add(pasutijumi);
break;
case "print":
print(pasutijumi);
break;
case "sum":
sum(pasutijumi);
break;
case "inc":
inc(pasutijumi);
break;
case "del":
delete(pasutijumi);
break;
case "done":
System.out.println("good bye");
break;
default:
System.out.println("unknown command");
break;
}
}
sc.close();
}
public static void print(HashMap<String, LinkedList<Prece>> pasutijumi) {
LinkedList<Prece> grozs;
for (String id : pasutijumi.keySet()) {
System.out.println("ID: " + id);
grozs = pasutijumi.get(id);
String str = String.format("%-20s%-10s%-10s", "nosaukums", "cena", "daudzums");
System.out.println(str);
for (Prece prece : grozs) {
prece.outputPrece();
}
}
}
public static void inc(HashMap<String, LinkedList<Prece>> pasutijumi) {
String id = sc.next();
String productName = sc.next();
double amount = sc.nextDouble();
LinkedList<Prece> grozs = pasutijumi.get(id);
if (grozs == null) {
System.out.println("unknown client");
} else {
boolean productFound = false;
for (Prece prece : grozs) {
if (prece.getNosaukums().equals(productName)) {
prece.setDaudzums(prece.getDaudzums() + (int) amount);
productFound = true;
break;
}
}
if (!productFound) {
System.out.println("not found");
}
}
}
public static void add(HashMap<String, LinkedList<Prece>> pasutijumi) {
LinkedList<Prece> grozs;
String id = sc.next();
Prece p = Prece.inputPrece(sc);
grozs = pasutijumi.get(id);
if (grozs != null) {
grozs.add(p);
} else {
grozs = new LinkedList<Prece>();
grozs.add(p);
pasutijumi.put(id, grozs);
}
}
public static void sum(HashMap<String, LinkedList<Prece>> pasutijumi) {
for (String id : pasutijumi.keySet()) {
LinkedList<Prece> grozs = pasutijumi.get(id);
if (grozs != null) {
double sum = 0.0;
for (Prece prece : grozs) {
sum += prece.getCena() * prece.getDaudzums();
}
System.out.println("ID: " + id + " sum: " + sum);
} else {
System.out.println("unknown client");
}
}
}
public static void delete(HashMap<String, LinkedList<Prece>> pasutijumi) {
String id = sc.next();
String nosaukums = sc.next();
LinkedList<Prece> grozs = pasutijumi.get(id);
if (grozs != null) {
int index = -1;
for (int i = 0; i < grozs.size(); i++) {
if (grozs.get(i).getNosaukums().equals(nosaukums)) {
index = i;
break;
}
}
if (index >= 0) {
Prece prece = grozs.remove(index);
System.out.println("Prece " + prece.getNosaukums() + " noņemta no klienta ar ID " + id + " pasūtījuma");
} else {
System.out.println("Prece " + nosaukums + " nav atrasta klienta ar ID " + id + " pasūtījumā");
}
} else {
System.out.println("Klienta ID " + id + " nav atrasts");
}
}
}
Sorry that the code is in latvian.
I have tried changing the list types and tried bug fixing however it still shows an error.
I am not understanding how recursive backtracking works. We did this example in class, but I don't understand why it backtracks when it is always adding 1 to level or i. Why don't we have to subtract at some point to actually go back through the array? Also, why do 2 recursive calls work, and not produce some kind of error?
public class Subsets {
public static void main(String[] args) {
int[] arr = {3,7,12};
printSubsets(arr);
}
public static void printSubsets(int[] a) {
boolean[] inSet = new boolean[a.length];
printSubsets(a, inSet, 0);
}
public static void printSubsets(int[] a, boolean[] inSet, int level) {
if(level == a.length) {
System.out.print("{");
int i = 0;
while(i < inSet.length && !inSet[i]) {
i++;
}
if(i < inSet.length) {
System.out.print(a[i]);
i++;
for(; i < inSet.length; i++) {
if(inSet[i])
System.out.print(", " + a[i]);
}
}
System.out.print("}");
System.out.println();
}
else {
//System.out.println(level + " " + inSet[level] + " ");
inSet[level] = false;
printSubsets(a, inSet, level+1);
inSet[level] = true;
printSubsets(a, inSet, level+1);
}
}
}
I am unable to optimise the following dynamic programming problem:
This is my JAVA solution for the problem with the following test cases.The limit gets exceeded on higher recursion levels. I am unable to understand how memoization can be implemented in this case. Any help would be appreciated.
import java.util.*;
public class ProblemD {
static class Fireworks{
boolean occupied;
int dir;
public Fireworks(boolean occupied,int dir){
this.occupied=occupied;
this.dir=dir;
}
}
public static void fireworks(Fireworks f[][],int i,int j,int levels[],int n){
if(n<levels.length){
for(int k=1;k<levels[n];k++){
if(f[i][j].dir==0){
f[i+1][j]=new Fireworks(true,0);
i=i+1;
}
else if(f[i][j].dir==45){
f[i+1][j-1]=new Fireworks(true,45);
i=i+1;
j=j-1;
}
else if(f[i][j].dir==90){
f[i][j-1]=new Fireworks(true,90);
j=j-1;
}
else if(f[i][j].dir==135){
f[i-1][j-1]=new Fireworks(true,135);
i=i-1;
j=j-1;
}
else if(f[i][j].dir==180){
f[i-1][j]=new Fireworks(true,180);
i=i-1;
}
else if(f[i][j].dir==225){
f[i-1][j+1]=new Fireworks(true,225);
i=i-1;
j=j+1;
}
else if(f[i][j].dir==270){
f[i][j+1]=new Fireworks(true,270);
j=j+1;
}
else{
f[i+1][j+1]=new Fireworks(true,315);
i=i+1;
j=j+1;
}
}
//n++;
if(f[i][j].occupied && n<levels.length-1){
if(f[i][j].dir==0){
f[i+1][j-1]=new Fireworks(true,45);
f[i+1][j+1]=new Fireworks(true,315);
fireworks(f,i+1,j-1,levels,n+1);
fireworks(f,i+1,j+1,levels,n+1);
return;
}
else if (f[i][j].dir==45){
f[i][j-1]=new Fireworks(true,90);
f[i+1][j]=new Fireworks(true,0);
fireworks(f,i,j-1,levels,n+1);
fireworks(f,i+1,j,levels,n+1);
return;
}
else if(f[i][j].dir==90){
f[i+1][j-1]=new Fireworks(true,45);
f[i-1][j-1]=new Fireworks(true,135);
fireworks(f,i+1,j-1,levels,n+1);
fireworks(f,i-1,j-1,levels,n+1);
return;
}
else if(f[i][j].dir==135){
f[i][j-1]=new Fireworks(true,90);
f[i-1][j]=new Fireworks(true,180);
fireworks(f,i,j-1,levels,n+1);
fireworks(f,i-1,j,levels,n+1);
return;
}
else if(f[i][j].dir==180){
f[i-1][j-1]=new Fireworks(true,135);
f[i-1][j+1]=new Fireworks(true,225);
fireworks(f,i-1,j-1,levels,n+1);
fireworks(f,i-1,j+1,levels,n+1);
return;
}
else if(f[i][j].dir==225){
f[i-1][j]=new Fireworks(true,180);
f[i][j+1]=new Fireworks(true,270);
fireworks(f,i-1,j,levels,n+1);
fireworks(f,i,j+1,levels,n+1);
return;
}
else if(f[i][j].dir==270){
f[i-1][j+1]=new Fireworks(true,225);
f[i+1][j+1]=new Fireworks(true,315);
fireworks(f,i-1,j+1,levels,n+1);
fireworks(f,i+1,j+1,levels,n+1);
return;
}
else {
f[i+1][j]=new Fireworks(true,0);
f[i][j+1]=new Fireworks(true,270);
fireworks(f,i+1,j,levels,n+1);
fireworks(f,i,j+1,levels,n+1);
return;
}
}
}
else{
return;
}
}
public static void main(String[] args){
Fireworks f[][];
f=new Fireworks[500][500];
for(int i=0;i<500;i++){
for(int j=0;j<500;j++){
f[i][j]=new Fireworks(false,0);
}
}
Scanner sc=new Scanner(System.in);
int start=250;
int n=sc.nextInt();
int levels[]=new int[n];
f[start][start].occupied=true;
f[start][start].dir=0;
for(int i=0;i<n;i++){
levels[i]=sc.nextInt();
}
fireworks(f,start,start,levels,0);
int count=0;
for(int i=0;i<500;i++){
for(int j=0;j<500;j++){
if(f[i][j].occupied){
System.out.println(i+" "+j);
count++;
}
}
}
System.out.println(count);
}
}
I have created the following but need the output line to maintain a consistent width to give the appearance of a race. I also need to ensure that the racers do not slip back past the start.
Currently, the output plots correctly, but the line only extends to the racers position.
Also, when there is a "slip" it appears that the racer sometimes moves back to start. I'm not sure if that is solved with a consistent length of "race track" as well.
import java.util.*;
public class Race {
public static void main(String []args) {
int finish=70,tort=1,hare=1,rtime=0;
System.out.println("ON YOUR MARK, GET SET\nBANG !!!!!\nAND THEY'RE OFF !!!!!\n");
do{
hare=movehare(hare);
tort=movetort(tort);
print(tort,hare);
rtime++;
}
while(tort<finish&&hare<finish);
if(tort>hare ){
System.out.println("\nTORTOISE WINS!\n");
}
else if(tort<hare ){
System.out.println("\nHARE WINS!\n");
}
else{
System.out.println("IT\'S A TIE!\n");
}
}
public static void print(int t,int h){
int i;
if(h==t){
for(i=0;i<h;i++)
System.out.print("_");
System.out.println("OUCH!!!");
}
else if(h<t){
for(i=0;i<h;i++)
System.out.print("_");
System.out.print("H");
for(i=0;i<(t-h);i++)
System.out.print("_");
System.out.print("T");
}
else{
for(i=0;i<t;i++)
System.out.print("_");
System.out.print("T");
for(i=0;i<(h-t);i++)
System.out.print("_");
System.out.print("H");
}
System.out.println();
}
public static int movehare(int r ){
int num;
num=(int)(Math.random()*10);
if(num<2){
r-=2;
}
else if(num<5){
r++;
}
else if(num<6){
r-=12;
}
else if(num<8){
r+=9;
}
if(r< 1 ){
r=1;
}
return r;
}
public static int movetort(int t){
int num;
num=(int)(Math.random()*10);
if(num<5){
t+=3;
}
else if(num<7){
t-= 6;
}
else{
t++;
}
if(t<1){
t=1;
}
return t;
}
}
You can print extra "_" after each of the IF branches in your print method
if(h != t){
for(i = Math.max(h,t); i < 70; i++){
System.out.print("_");
}
}
Note: The line breaks will be off when the H == T because you print "Ouch!!!", so you'll have to check for that and accommodate.
Tonight we have to submit a project contains many methods,and one of them is to print a diamond with only odd numbers with spaces between them to have an output like this if the number was 3 :
*
* * *
*
public static void drawDiamond(int n)
{
int q = n/2;
for(int a = -q; a <= q ;a++) {
int b = (a < 0)?-a:a;
int c = q-b;
for(int d = 0;d <= q+c;d++) {
System.out.print((d < b)?" ":"*");
}
System.out.println();
}
}
well,I tried to write a method myself but I couldn't,just found this one and I need someone to help me to convert it to a recursive method..and thank you very much <3
public static String DiamondF(int num) //method1
{
if(num>0)
{
return "* " + DiamondF(num+2);
}
else
{
return " ";
}
}
public static String DiamondS(int num)//method2
{
if(num>0)
{
return " * " + DiamondF(num+2);//access method1
}
else
{
return " ";
}
}
public static String Space(int num) //method3
{
if(num>0)
{
return " " + Space(num+2);
}
else
{
return " ";
}
}
public static void DiamondResult(int num)//method4
{
for(int i=1; i<num; i++)
{
System.out.print(Space(num-i));//access method3
System.out.println(DiamondF(i));//access method1
}
for(int i=0; i<num; i++)
{
System.out.println(DiamondS(num-i));//access method2
System.out.print(Space(i));//access method3
}
}
this one is totally wrong and gives me excaptions !
public static String DiamondF(int
num) //method1
{
if(num>0)
{
return "* " +
DiamondF(num+2);
}
else
{
return " ";
}
}
public static String DiamondS(int
num)//method2
{
if(num>0)
{
return " * " +
DiamondF(num+2);//access method1
}
else
{
return " ";
}
}
public static String Space(int num)
//method3
{
if(num>0)
{
return " " + Space(num+2);
}
else
{
return " ";
}
}
public static void DiamondResult(int
num)//method4
{
for(int i=1; i<num; i++)
{
System.out.print(Space(num-i));//access method3
System.out.println(DiamondF(i));//access method1
}
for(int i=0; i<num; i++)
{
System.out.println(DiamondS(num-i));//access method2
System.out.print(Space(i));//access method3
}
}
Its very hard to understand what you want, cause your description
to print a diamond with only odd numbers with spaces between them
and the figure you've posted
*
* * *
*
don't really match. Anyways, here is a very simple recursive code that prints a diamond, understand it, and work from there
static int iteration = 1;
private static void drawDiamond(int n) {
String row = new String(new char[iteration]).replace("\0", "*"); // create a string with '*' repating n times
iteration++;
if (iteration <= n) {
System.out.println(row);
drawDiamond(n);
}
System.out.println(row);
}
prints
*
**
***
**
*