仍然是整周编程的新手。一直在编写小型程序以帮助使基本知识陷入困境。当我运行此代码时,如果我输入的第一个问题正确无误,则它可以正确运行,但是如果我回答错了,然后又循环又回到正确的位置,它将继续循环而不继续其余代码。>
import java.util.Scanner;
public class Parker {
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
System.out.print("How old are you: ");
int age = scan.nextInt();
if (age > 6) {
System.out.println("You are to old for the program");
}
else {
System.out.println("Welcome to the program, enjoy!");
System.out.println("What is 5 + 3? ");
int num1 = scan.nextInt();
if (num1 == 8) {
System.out.println("Correct!");
} else {
System.out.println("Wrong! I'm telling your DADDY!");
}
while (num1 != 8) {
System.out.println("What is 5 + 3? ");
int num9 = scan.nextInt();
if (num9 == 8) {
System.out.println("Correct!");
} else {
System.out.println("Wrong! I'm telling your DADDY!");
}
}
System.out.println("What is 3 + 11? ");
int num2 = scan.nextInt();
if (num2 == 14) {
System.out.println("Correct!");
} else {
System.out.println("Wrong! I'm telling your DADDY!");
}
System.out.println("What is 7 + 6? ");
int num3 = scan.nextInt();
if (num3 == 13) {
System.out.println("Correct!");
}
else {
System.out.println("Wrong! I'm telling your DADDY!");
}
System.out.println("What is 11 - 5? ");
int num4 = scan.nextInt();
if (num4 == 6) {
System.out.println("Correct!");
}
else {
System.out.println("Wrong! I'm telling your DADDY!");
}
System.out.println("What is 4 + 9? ");
int num5 = scan.nextInt();
if (num5 == 13) {
System.out.println("Correct!");
}
else {
System.out.println("Wrong! I'm telling your DADDY!");
}
}
}
}
答案 0 :(得分:2)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sidebar Test</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
}
.sidebar-content {
display: none;
}
.sidebar-header {
text-align: center;
}
#sidebar-interact {
background: white;
border: 2px solid #BAB3AD;
border-radius: 8px;
height: 50%;
overflow-y: auto;
width: 460px;
margin: 2px;
}
</style>
<script>
var sidebar;
var sidebarButton;
function initMap()
{
var map = L.map('map', {
center: [51, 0],
zoom: 16,
zoomControl: false
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 20,
attribution: 'Imagery © <a href="https://www.mapbox.com/" target="_blank">Mapbox</a>',
id: 'mapbox.streets',
}).addTo(map);
L.Control.Sidebar = L.Control.extend({
initialize: function(id) {
var options = {
position: 'topleft',
};
L.setOptions(this, options);
this._sidebar = L.DomUtil.get('sidebar-' + id);
L.DomUtil.addClass(this._sidebar, 'sidebar-left');
if(L.Browser.touch)
{
L.DomUtil.addClass(this._sidebar, 'leaflet-touch');
}
this._content = this._sidebar.children[0];
var closeButton = "<center><input type='button' value='Close sidebar' onclick='sidebar.hide();'></center>";
this._content.innerHTML += closeButton;
},
isVisible: function() {
return L.DomUtil.hasClass(this._container, 'visible');
},
show: function() {
if(!this.isVisible())
{
L.DomUtil.addClass(this._container, 'visible');
L.DomUtil.get("sidebar-interact").style = "display: block;";
L.DomUtil.get("sidebar-button").style = "display: none;";
}
},
hide: function() {
if(this.isVisible())
{
L.DomUtil.removeClass(this._container, 'visible');
L.DomUtil.get("sidebar-interact").style = "display: none;";
L.DomUtil.get("sidebar-button").style = "display: inline;";
}
},
onAdd: function() {
var div = L.DomUtil.create('div', 'command');
div.innerHTML = this._content.innerHTML;
div.id = "sidebar-interact";
div.style = "display: none;";
return div;
}
});
sidebar = new L.Control.Sidebar("1").addTo(map);
L.Control.SidebarButton = L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function() {
var div = L.DomUtil.create('div', 'command');
div.innerHTML = "<input type='button' id='myButton' value='Show sidebar' onclick='sidebar.show();'>";
div.id = "sidebar-button";
return div;
}
});
sidebarButton = new L.Control.SidebarButton().addTo(map);
L.DomEvent.disableClickPropagation(document.getElementById("sidebar-interact"));
L.DomEvent.disableScrollPropagation(document.getElementById("sidebar-interact"));
}
</script>
</head>
<body onload='initMap();'>
<div id="sidebar-1" class="sidebar collapsed">
<div class="sidebar-content">
<h1 class="sidebar-header">
My Header
</h1>
<p>A responsive sidebar for mapping libraries like <a href="http://leafletjs.com/">Leaflet</a> or <a href="http://openlayers.org/">OpenLayers</a>.</p>
<p class="lorem">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p class="lorem">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p class="lorem">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p class="lorem">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p> Testing text</p>
<p> Even more text</p>
</div>
</div>
<div id="map" class='sidebar-map'></div>
</body>
</html>
代码中的上述条件取决于while (num1 != 8) {
System.out.println("What is 5 + 3? ");
int num9 = scan.nextInt();
if (num9 == 8) {
System.out.println("Correct!");
} else {
System.out.println("Wrong! I'm telling your DADDY!");
}
}
,但是您绝不会在该循环内更改num1
的值。因此,该循环将永远不会终止。
答案 1 :(得分:0)
更改此内容
while (num1 != 8) {
System.out.println("What is 5 + 3? ");
int num9 = scan.nextInt();
if (num9 == 8) {
System.out.println("Correct!");
} else {
System.out.println("Wrong! I'm telling your DADDY!");
}
对此,
while (num1 != 8) {
System.out.println("What is 5 + 3? ");
int num9 = scan.nextInt();
if (num9 == 8) {
System.out.println("Correct!");
break;
} else {
System.out.println("Wrong! I'm telling your DADDY!");
}
顺便说一句,您应该只使用一个变量从扫描仪获取输入,以避免造成混淆。
答案 2 :(得分:-2)
这里有!
import java.util.Scanner;
public class Parker {
public static void main(String []args) {
int numl = 0;
Scanner scan = new Scanner(System.in);
System.out.print("How old are you: ");
int age = scan.nextInt();
if (age > 6) {
System.out.println("You are to old for the program");
}
else {
System.out.println("Welcome to the program, enjoy!");
System.out.println("What is 5 + 3? ");
numl = scan.nextInt();
while(numl != 8) {
System.out.println("Wrong! I'm telling your DADDY!");
System.out.println("What is 5 + 3? ");
numl = scan.nextInt();
}
System.out.println("Correct!");
System.out.println("What is 3 + 11? ");
numl = scan.nextInt();
while(numl != 14) {
System.out.println("Wrong! I'm telling your DADDY!");
System.out.println("What is 3 + 11? ");
numl = scan.nextInt();
}
System.out.println("Correct!");
System.out.println("What is 7 + 6? ");
numl = scan.nextInt();
while(numl != 13) {
System.out.println("Wrong! I'm telling your DADDY!");
System.out.println("What is 7 + 6? ");
numl = scan.nextInt();
}
System.out.println("Correct!");
System.out.println("What is 11 - 5? ");
numl = scan.nextInt();
while(numl != 6) {
System.out.println("Wrong! I'm telling your DADDY!");
System.out.println("What is 11 - 5? ");
numl = scan.nextInt();
}
System.out.println("Correct!");
System.out.println("What is 4 + 9? ");
numl = scan.nextInt();
while(numl != 13) {
System.out.println("Wrong! I'm telling your DADDY!");
System.out.println("What is 4 + 9? ");
numl = scan.nextInt();
}
System.out.println("Correct!");
}
}
}