问:如何在方法中打破for循环?

时间:2017-05-14 12:24:20

标签: java loops break

所以我试图找出数组a是否是数组b的子集,我的代码如下:`

public class subset {
    public static boolean subset(int[]a, int[] b) {
        for(int i=0;i<a.length;i++){
            for(int j=0;i<a.length;j++){
                if(a[i]==b[j]){
                    return true;
                    break;
                }else{
                    return false;
                }
            }
        }
    }
    public static void main(String[]args){
        int[] a = {1,2,6};
        int[] b = {1,2,4,3,7,4,8,5};
        if (subset(a,b))
            System.out.println("Array 1 is  contained  in Array 2");
        else
            System.out.println("Array 1 is not contained in Array 2");
    }
}

我收到以下错误:无法访问的语句和缺少的return语句。 我想要做的是,当if条件为真时,它会停止内部for循环并继续使用外部for循环。 提前致谢

2 个答案:

答案 0 :(得分:0)

需要修复2件事。您需要删除return false,并在结尾处添加public static boolean subset(int[]a, int[] b) { for(int i=0;i<a.length;i++){ for(int j=0;i<a.length;j++){ if(a[i]==b[j]){ return true; //break; remove this }else{ return false; } } } return false; //add this }

if..else

此外,您可以通过这样做而不是return a[i]==b[j];

来简化它
<html lang="en">
<head>
  <title>PHP - jquery ajax crop image before upload using croppie plugins</title>
  <script src="http://demo.itsolutionstuff.com/plugin/jquery.js"></script>
  <script src="http://demo.itsolutionstuff.com/plugin/croppie.js"></script>
  <link rel="stylesheet" href="http://demo.itsolutionstuff.com/plugin/bootstrap-3.min.css">
  <link rel="stylesheet" href="http://demo.itsolutionstuff.com/plugin/croppie.css">
</head>
<body>

<div class="container">
	<div class="panel panel-default">
	  <div class="panel-heading">Image Cropping Area</div>
	  <div class="panel-body">

	  	<div class="row">
	  		<div class="col-md-4 text-center">
				<div id="upload-demo" style="width:350px"></div>
	  		</div>
	  		<div class="col-md-4" style="padding-top:30px;">
				<strong>Select Image:</strong>
				<br/>
				<input type="file" id="upload">
				<br/>
				<button class="btn btn-success upload-result">Upload Image</button>
	  		</div>
  		  <div class="col-md-4" style="">
			  <div id="upload-demo-i" style="background:#e1e1e1;width:300px;padding:30px;height:300px;margin-top:30px"></div>
	  		</div>
            <div>
    <a name="Download Save Image" id="download" download>Download Save Image</a>
</div>
	  	</div>

	  </div>
	</div>
</div>

<script type="text/javascript">
$uploadCrop = $('#upload-demo').croppie({
    enableExif: true,
    viewport: {
        width: 200,
        height: 200,
        type: 'circle'
    },
    boundary: {
        width: 300,
        height: 300
    }
});

$('#upload').on('change', function () { 
	var reader = new FileReader();
    reader.onload = function (e) {
    	$uploadCrop.croppie('bind', {
    		url: e.target.result
    	}).then(function(){
    		console.log('jQuery bind complete');
    	});
    	
    }
    reader.readAsDataURL(this.files[0]);
});

$('.upload-result').on('click', function (ev) {
	$uploadCrop.croppie('result', {
		type: 'canvas',
		size: 'viewport'
	}).then(function (resp) {

		$.ajax({
			url: "ajaxpro.php",
			type: "POST",
			data: {"image":resp},
			success: function (data) {
				console.log(data);
                var response = JSON.parse(data);
                if (response.image) {
                    html = '<img id="preview" src="' + response.image + '" />';
                    $("#upload-demo-i").html(html);
                    $('#download').attr({
                        target: '_blank',
                        href: response.image
                    })
                }else {
                    alert('Failed to upload image');
                }
				
			}
		});
	});
});





</script>

</body>
</html>

答案 1 :(得分:0)

第一个问题是:

return true;
break;}

第二行永远不会被执行,因为第一行终止方法执行。在这里决定你想要哪一行:返回一个值或跳出内部循环(我不认为,这个分支应该是空的,见下文)。

第二个问题是,当您传入一个空数组时,不会执行循环,并且该方法没有返回值。这里只需在方法的末尾添加一个“return true”(或者为false,非常随意)。

你可能会以这种方式简化循环体:

if(a[i]!=b[j]){
    return false;
}