Tag: 无限循环

Java:递归-While Loop Vs If Loop

代码设计1:完美运行 public static void main (String[] args) { recursion(2); } public static void recursion(int num) { if (num > 0) { recursion( num – 1 ); System.out.println(num); } } 代码设计2:无限循环。 ? public static void main (String[] args) { recursion(2); } public static void recursion(int num) { if (num == 0) return; while (num > 0) […]

while循环和线程的无限循环问题

使用一个基本的例子来说明我的问题,我有两个几乎相同的代码位。 此代码使while循环无限运行。 private boolean loadAsset() { new Thread(new Runnable() { @Override public void run() { // Do something loaded = true; } }).start(); while (!loaded) { // System.out.println(“Not Loaded”); } System.out.println(“Loaded”); return false; } 但是这个代码(即在while循环中执行某些操作)会导致已成功计算loaded变量,并允许while循环中断并完成方法。 private boolean loadAsset() { new Thread(new Runnable() { @Override public void run() { // Do something loaded = true; } […]

对于Java中没有参数的循环

我正在看别人的代码,我找到了这段代码: for (;;) { 我不是Java专家; 这行代码在做什么? 起初,我认为它会创建一个无限循环,但在这个程序员使用的SAME类中 while(true) 哪个(纠正我,如果我错了)是一个无限循环。 这两个相同吗? 为什么有人会改变他们的方法重复相同的过程? 任何见解都有帮助, 谢谢!

如何中断无限循环

虽然我知道提问会有点傻,但我还是想更多地询问它的技术观点。 无限循环的一个简单示例: public class LoopInfinite { public static void main(String[] args) { for (;;) { System.out.println(“Stack Overflow”); } } } 如何从这个类的外部中断(停止)这个无限循环(例如,在inheritance的帮助下)?

尝试catch阻塞导致无限循环?

我正在写一个简单的java控制台游戏。 我使用扫描仪从控制台读取输入。 我试图validation它我要求一个整数,如果输入一个字母,我不会收到错误。 我试过这个: boolean validResponce = false; int choice = 0; while (!validResponce) { try { choice = stdin.nextInt(); validResponce = true; } catch (java.util.InputMismatchException ex) { System.out.println(“I did not understand what you said. Try again: “); } } 但它似乎创建了一个无限循环,只是打印出catch块。 我究竟做错了什么。 是的,我是Java新手

而“!b.equals(x)|| !b.equals(y)“是一个无限循环?

嘿伙计这是我的代码,它正在做的是循环,但它想要做的是如果用户键入借用然后它将询问用户它做了多少但然后他们输入一个数字,它将再次问他们你想借还是卖,它是无限循环的。 case 3: do{ System.out.println(“What would you like to do? Please type borrow to borrow money or sell to sell assets: “); b = scan.nextLine().toLowerCase(); if(b.equals(“borrow”)){ System.out.print(“how much would you like to borrow Remmber if you go over 50000 debt its game over.”); try { input = scan.nextInt(); } catch (Exception e) { System.err.println(“That is […]

如何在Java中使用Json序列化时避免无限循环

我使用hibernate检索兄弟列表 public class Brother { public int brotherId; public string name; public List brothers; public Brother() { brothers = new ArrayList(); } //Getter Setter } Hibernate是在兄弟列表中使用lazy select配置的,这在Java端工作,但问题是当我想将一个Brother对象序列化为JSON时。 I’ve got org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) 例如布莱恩可以让马克作为一个反面的兄弟…… 我怎么解决呢? 有没有办法表明jackson图书馆的最大递归数? 我的代码,真的很简单。 Brother brother = this.myservice.getBrother(4); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(brother));

Java:为什么这段代码不起作用? 无限循环?

因此,您可以从我的尝试中得知,我正在试图弄清楚我如何创建一个程序,让用户在5秒内输入一些文本行,然后扫描程序将计算输入的行数。 我刚刚开始学习Java作为我的第二语言,所以请尽可能简单地解释一切:) 我有两个理论说明为什么它不起作用。 第一个是nextLine()将返回整行,无论它是否为空,而不是NL等于“”,它实际上将等于整行(即“”)。 我的第二个理论是,我不知道我在做什么,程序流程到处都是。 无论如何,这是我的代码: class OrigClass{ public static void main(String args[]){ Scanner ScanObj = new Scanner(System.in); int Count = 0; String NL = ScanObj.nextLine(); try{ Thread.sleep(5000);} catch (InterruptedException e){ e.printStackTrace(); } while (!NL.equals(“”)){ Count++; NL = ScanObj.nextLine(); } System.out.print(“You Entered ” + Count + ” Lines.”); ScanObj.close(); } } 哦,我忘了提到hasNext()是我最初的尝试: import java.util.Scanner; class […]

Java Filter无限循环

我想实现一个filter来进行身份validation,但不知怎的,它被卡在无限循环中…任何想法都赞赏。 HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; doBeforeProcessing(request, response); Throwable problem = null; HttpSession session = httpRequest.getSession(true); if(session.getAttribute(“userName”)!=null&&session.getAttribute(“userName”)!=(“”)) { try { chain.doFilter(request, response); } catch (Throwable t) { // If an exception is thrown somewhere down the filter chain, // we still want to execute our after processing, and then // […]

使用扫描仪时无限循环?

boolean z = false; do { try { a = sc.nextInt(); z = true; } catch(Exception e) { } } while(!z); 尝试这个。 如果在第一次正确执行时尝试整数。 但是,如果输入错误的文本类型,即使您输入int next并跳过将布尔值赋值为true,它也会变为无限循环。 为什么是这样?