输入无效输入时,几何程序是否会突然失控?

时间:2018-04-25 17:19:21

标签: scala

我正在为课程做一个解决一些基本几何问题的作业。

我有大部分代码都在工作,除非用户输入无效选项,发生无限循环,我无法弄清楚如何结束它。这是我的代码:

import scala.io.StdIn._

// Ask the user which problem to do
println("Please select a problem to solve.")
println("  0. Point-Circle test.")
println("  1. Circle-Circle intersection.")
println("  2. Square-Square intersection.")
println("  3. Quit.")
var choice = readInt


while (choice != 3) {

// Process the choice
if (choice == 0) {
  // Determine if circle-point intersection exists
  processPointInCircle()
} else if (choice == 1) {
  // Determine if circle-circle intersection exists
  processCircleCircle()
} else if (choice == 2) {
  // Determine if square-square intersection exists
  processSquareSquare()
} else if (choice == 3) {
  // We are done
} else {
  println("Invalid command.")
  }
}

//Functions for solving each problem

def processPointInCircle() {
  println("Please enter the values for the circle: CX, CY, and Radius.")
  val cx = readDouble
  val cy = readDouble
  val radius = readDouble

  println("Please enter the values for the point: PX, PY.")
  val px = readDouble
  val py = readDouble

  val dx = px - cx
  val dy = py - cy

  val magnitude = Math.sqrt(dx * dx + dy * dy)
  if (magnitude <= radius) {
    println("The point lies in the circle.")
  }

  else {
    println("The point does not lie in the circle.")
  }

  println("Please select a problem to solve.\n" +
  "  0. Point-Circle test.\n" +
  "  1. Circle-Circle intersection.\n" +
  "  2. Square-Square intersection.\n" +
  "  3. Quit.")
  choice = readInt
}

def processCircleCircle() {
  println("Please enter the values for circle A: CX, CY, and Radius.")
  val x1 = readDouble
  val y1 = readDouble
  val r1 = readDouble
  println("Please enter the values for circle B: CX, CY, and Radius.")
  val x2 = readDouble
  val y2 = readDouble
  val r2 = readDouble
  val dx = x2-x1
  val dy = y2-y1
  val d = Math.sqrt((dx*dx)+(dy*dy))
  val R = r1+r2
  val D = d-R
    if (D>0) {
      println("The two circles do not intersect.")
    }
    else  {
      println("The two circles intersect.")
    }
    println("Please select a problem to solve.\n" +
        "  0. Point-Circle test.\n" +
        "  1. Circle-Circle intersection.\n" +
        "  2. Square-Square intersection.\n" +
        "  3. Quit.")
        choice = readInt
}

def processSquareSquare() {
  println("Please enter the values for square A: Left X, Bottom Y, and Side Length.")
  val xa = readDouble
  val ya = readDouble
  val la = readDouble
  println("Please enter the values for square B: Left X, Bottom Y, and Side Length.")
  val xb = readDouble
  val yb = readDouble
  val lb = readDouble
    if (xa+la<xb || xb+lb<xa || ya+la<yb || yb+lb<ya) {
      println("The two squares do not intersect.")
    }
    else {
      println("The two squares intersect.")
    }
  println("Please select a problem to solve.\n" +
  "  0. Point-Circle test.\n" +
  "  1. Circle-Circle intersection.\n" +
  "  2. Square-Square intersection.\n" +
  "  3. Quit.")
  choice = readInt
}

我知道我的错误发生在“while(choice!= 3)”的位置,但我不知道如何解决这个问题,因为我仍然需要循环其他选项。

我尝试从标准库导入一个允许中断循环的实用程序,但是当将程序上传到Mimir时,它仍然无法通过测试,因此我无法使用它。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你只需稍微改变你的逻辑。当用户输入未识别的输入时,例如4,你应该让他们重新进入一个新的选择。

希望这有帮助。

<强> 代码

select * from public_key

select DATEDIFF(SECOND,{d '1970-01-01'}, GETDATE())

insert into public_key
  (owner_id, [name], fingerprint, content, mode, [type], created_unix, updated_unix)
  select 99, 'Umbraco Fyin.com', 'SHA256:NlTXCqPAfBq5VJcZsqpt0kvAFCcjmCs36oE88nhRT64', 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDMfRSzRFSlHjGmOmJfj+6IdmbBWjdXogf3+OVoHoFWMyTOmsKnHE4DSjWIlxnndnxE8jbxCQhoN1tYamwslBJz4MocFju2SFwy3pEbXeP4Zi80Iup3BkvKr40HifAeYS9s29aPum9tMGWOAWZCNOqO7pjzAEmdi+4x3HG4m07/j/Une2SBN3qgk3OaN3ZLhysbL52tR2v+ILgQYb4dZjCCC6SFmCaXBDOSebZ+fP1QDv5mFDyyv0OIePs/qwadfA42sRyAevn8dyrx2mIy9/UmZVOGmzS7tFGK1f6bRe4WLNP71ijv6xpafmSiwk6W3auCEXCYsUCNbMQ6k+KDa08MfgcMPQYQF08XXUVfW6bgdbljWrVDbCu8lGGz5KOP/IfLdOmc/Mdkr+g+R3gB5h55w1spJfxLmbXNoXi8SvfPEp8KZzyZuejhlUV7vO4UsI39VMH9WjfPExqFUsEG7lA6lzAvs2W2gNp3l0+YVxXAJgVdJbPuc/bRaW0J7VDLrpC/AAtDgCc10lwuCE7AJqY2P9alyl7PUJDtAB1YfbsRjr8Cpn0+9ed3chDm/NxWMf2oN5j92Qz3H4pqUJ/AOIlKl2UZgNkiKm4d325DqncIgd19pNwl3D7EHxHIWkW+jwDQp22WJAzhIq+vbQRpmtSZ113j4f14JvUPL0eM83WTfQ== youremail@fyin.com', 2, 1, 1524623418, 0`
相关问题